SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases. It is a standard language used by most database management systems (DBMS) such as MySQL, Oracle, and Microsoft SQL Server. SQL queries are used to retrieve, insert, update, and delete data from a database.
SQL queries are written in a specific syntax that follows a set of rules and guidelines. The basic structure of an SQL query consists of a SELECT statement, which specifies the columns to be retrieved from a table, and a FROM statement, which specifies the table from which the data is to be retrieved. Additional clauses such as WHERE, GROUP BY, and ORDER BY can be added to filter, group, and sort the data.
For example, the following SQL query retrieves all the records from a table named “customers”:
SELECT * FROM customers;
This query retrieves all the columns and rows from the “customers” table. The asterisk (*) is a wildcard character that represents all columns in the table.
SQL queries can also be used to insert new data into a table, update existing data, and delete data from a table. For example, the following SQL query inserts a new record into the “customers” table:
INSERT INTO customers (name, email, phone) VALUES ('John Doe', 'johndoe@email.com', '555-1234');
This query inserts a new record with the name, email, and phone values specified into the “customers” table.
SQL queries are an essential part of managing and manipulating data in a relational database. They allow users to retrieve, insert, update, and delete data from a database using a standard syntax that is widely used across different database management systems.
This cheat sheet provides an extensive list of SQL queries and their descriptions. The queries are divided into different categories for easy reference.
SELECT Queries
Query | Description |
---|---|
SELECT column1, column2, … FROM table_name; | Selects specific columns from a table |
SELECT * FROM table_name; | Selects all columns from a table |
SELECT DISTINCT column_name FROM table_name; | Selects unique values from a column |
SELECT column1, column2, … FROM table_name WHERE condition; | Selects specific columns from a table based on a condition |
SELECT column1, column2, … FROM table_name ORDER BY column_name ASC/DESC; | Selects specific columns from a table and orders them in ascending or descending order |
SELECT column1, column2, … FROM table_name LIMIT number; | Selects specific columns from a table and limits the number of rows returned |
WHERE Clauses
Query | Description |
---|---|
WHERE column_name = value; | Selects rows where the column value is equal to the specified value |
WHERE column_name <> value; | Selects rows where the column value is not equal to the specified value |
WHERE column_name > value; | Selects rows where the column value is greater than the specified value |
WHERE column_name < value; | Selects rows where the column value is less than the specified value |
WHERE column_name >= value; | Selects rows where the column value is greater than or equal to the specified value |
WHERE column_name <= value; | Selects rows where the column value is less than or equal to the specified value |
WHERE column_name LIKE ‘value%’; | Selects rows where the column value starts with the specified value |
WHERE column_name LIKE ‘%value’; | Selects rows where the column value ends with the specified value |
WHERE column_name LIKE ‘%value%’; | Selects rows where the column value contains the specified value |
WHERE column_name IN (value1, value2, …); | Selects rows where the column value is equal to any of the specified values |
WHERE column_name NOT IN (value1, value2, …); | Selects rows where the column value is not equal to any of the specified values |
WHERE column_name BETWEEN value1 AND value2; | Selects rows where the column value is between the specified values |
WHERE column_name IS NULL; | Selects rows where the column value is null |
WHERE column_name IS NOT NULL; | Selects rows where the column value is not null |
JOIN Queries
Query | Description |
---|---|
SELECT column1, column2, … FROM table1 JOIN table2 ON table1.column_name = table2.column_name; | Joins two tables based on a common column |
SELECT column1, column2, … FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name; | Joins two tables based on a common column and includes all rows from the left table |
SELECT column1, column2, … FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name; | Joins two tables based on a common column and includes all rows from the right table |
SELECT column1, column2, … FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name; | Joins two tables based on a common column and includes all rows from both tables |
GROUP BY Queries
Query | Description |
---|---|
SELECT column1, column2, … FROM table_name GROUP BY column_name; | Groups rows based on a column |
SELECT column1, column2, … FROM table_name GROUP BY column_name HAVING condition; | Groups rows based on a column and applies a condition to the groups |
Aggregate Functions
Query | Description |
---|---|
SELECT COUNT(column_name) FROM table_name; | Counts the number of rows in a column |
SELECT SUM(column_name) FROM table_name; | Calculates the sum of values in a column |
SELECT AVG(column_name) FROM table_name; | Calculates the average of values in a column |
SELECT MAX(column_name) FROM table_name; | Finds the maximum value in a column |
SELECT MIN(column_name) FROM table_name; | Finds the minimum value in a column |
Subqueries
Query | Description |
---|---|
SELECT column1, column2, … FROM table_name WHERE column_name IN (SELECT column_name FROM table_name WHERE condition); | Uses a subquery to select rows based on a condition |
SELECT column1, column2, … FROM table_name WHERE column_name = (SELECT column_name FROM table_name WHERE condition); | Uses a subquery to select rows based on a condition |