SQL Statements Guide(Cheat-sheet)

SQL, or Structured Query Language, is a programming language used to manage data stored in relational databases. A relational database is a database that organizes information into one or more tables, which are collections of data organized into rows a…


This content originally appeared on DEV Community and was authored by Barbra Mududa

SQL, or Structured Query Language, is a programming language used to manage data stored in relational databases. A relational database is a database that organizes information into one or more tables, which are collections of data organized into rows and columns. SQL statements are used to manage and manipulate data in these tables.

1. CREATE TABLE

One of the most basic SQL statements is the CREATE TABLE statement, which is used to create a new table. For example:

CREATE TABLE users (
   id INTEGER PRIMARY KEY,
   name TEXT,
   age INTEGER
);

2. INSERT

The INSERT statement is used to insert a new row into a table. For example:

INSERT INTO users (name, age)
VALUES 
     ('John', 30),
     ('Jane', 25);

3. ALTER TABLE

The ALTER TABLE statement is used to add a new column to a table. For example:

ALTER TABLE users 
ADD COLUMN email TEXT;

4. UPDATE

The UPDATE statement is used to edit a row in a table. For example:

UPDATE users 
SET age = 31 
WHERE name = 'John';

5. DELETE FROM

The DELETE FROM statement is used to delete one or more rows from a table. For example:

DELETE FROM users 
WHERE age < 25;

6. AS

The AS keyword allows you to rename a column or table using an alias. For example:

SELECT name AS 'Full Name', age AS 'Age'
FROM users;

7. Constraints

Constraints are used to add information about how a column can be used, such as UNIQUE, NOT NULL, and DEFAULT. For example:

CREATE TABLE employees (
   id INTEGER PRIMARY KEY,
   name TEXT UNIQUE,
   age INTEGER NOT NULL,
   salary INTEGER DEFAULT 50000
);

8. DISTINCT

The DISTINCT keyword is used to return unique values in the output. For example:

SELECT DISTINCT age
FROM employees;

9. WHERE

The WHERE clause is used to restrict query results to only the information we want. For example:

SELECT *
FROM employees
WHERE age > 30;

10.LIKE

The LIKE operator is used to search for a specific pattern in a column. The percentage sign (%) is a wildcard character that matches zero or more missing letters in the pattern. For example:

SELECT *
FROM employees
WHERE name LIKE 'J%';

11. 'IS NULL' AND 'IS NOT NULL'

The IS NULL and IS NOT NULL operators are used to filter the result set to only include rows with null or non-null values in a column, respectively. For example:

SELECT name
FROM employees 
WHERE salary IS NOT NULL;

12. BETWEEN

The BETWEEN operator is used to filter the result set within a certain range. For example:

SELECT *
FROM employees
WHERE age BETWEEN 25 AND 35;

13. AND and OR

The AND and OR operators are used to combine multiple conditions in a WHERE clause to make the result set more specific and useful. For example:

SELECT *
FROM employees
WHERE age BETWEEN 25 AND 35
   AND salary > 60000
   OR name LIKE '%Smith%';

14. ORDER BY

The ORDER BY statement is used to sort the result set in ascending or descending order. For example:

SELECT *
FROM employees
ORDER BY age DESC;

15. LIMIT

The LIMIT statement is used to specify the maximum number of rows the result set will have. For example:

SELECT *
FROM employees
LIMIT 10;

16. CASE

The CASE statement is used to create different outputs based on a condition. For example:

SELECT name,
 CASE
  WHEN age > 30 THEN 'Senior Employee'
  WHEN age > 25 THEN 'Mid-Level Employee'
  ELSE 'Junior Employee'
 END
FROM employees;

In conclusion,SQL is an extremely powerful tool for managing and manipulating data stored in relational databases.


This content originally appeared on DEV Community and was authored by Barbra Mududa


Print Share Comment Cite Upload Translate Updates
APA

Barbra Mududa | Sciencx (2023-05-06T16:30:22+00:00) SQL Statements Guide(Cheat-sheet). Retrieved from https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/

MLA
" » SQL Statements Guide(Cheat-sheet)." Barbra Mududa | Sciencx - Saturday May 6, 2023, https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/
HARVARD
Barbra Mududa | Sciencx Saturday May 6, 2023 » SQL Statements Guide(Cheat-sheet)., viewed ,<https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/>
VANCOUVER
Barbra Mududa | Sciencx - » SQL Statements Guide(Cheat-sheet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/
CHICAGO
" » SQL Statements Guide(Cheat-sheet)." Barbra Mududa | Sciencx - Accessed . https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/
IEEE
" » SQL Statements Guide(Cheat-sheet)." Barbra Mududa | Sciencx [Online]. Available: https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/. [Accessed: ]
rf:citation
» SQL Statements Guide(Cheat-sheet) | Barbra Mududa | Sciencx | https://www.scien.cx/2023/05/06/sql-statements-guidecheat-sheet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.