Quick SQL Cheatsheet: SELECT: Used To Select Data From A Database
Quick SQL Cheatsheet: SELECT: Used To Select Data From A Database
A quick reminder of all relevant SQL queries and examples on how to use them.
This repository is constantly being updated and added to by the community. Pull
requests are welcome. Enjoy!
Table of Contents
1. Finding Data Queries.
2. Data Modification Queries.
3. Reporting Queries.
4. Join Queries.
5. View Queries.
6. Altering Table Queries.
7. Creating Table Query.
• SELECT column_names FROM table_name WHERE column_name BETWEEN value1 AND val
ue2;
• SELECT * FROM Products WHERE (column_name BETWEEN value1 AND value2) AND
NOT column_name2 IN (value3, value4);
• SELECT * FROM Products WHERE column_name BETWEEN #01/07/1999# AND
#03/12/1999#;
• Each SELECT statement within UNION must have the same number of columns
• The columns must have similar data types
• The columns in each SELECT statement must also be in the same order
• SELECT columns_names FROM table1 UNION SELECT column_name FROM table2;
• UNION operator only selects distinct values, UNION ALL will allow duplicates
EXCEPT: set operator used to return all the records in the first
SELECT statement that are not found in the second SELECT
statement
• The ANY operator returns true if any subquery values meet the condition
• The ALL operator returns true if all subquery values meet the condition
• SELECT columns_names FROM table1 WHERE column_name operator (ANY|ALL)
(SELECT column_name FROM table_name WHERE condition);
• SELECT column_name1,
COUNT(column_name2) FROM table_name WHERE condition GROUP
BY column_name1 ORDER BY COUNT(column_name2) DESC;
3. Reporting Queries
COUNT: returns the # of occurrences
4. Join Queries
INNER JOIN: returns records that have matching value in both
tables
LEFT (OUTER) JOIN: returns all records from the left table
(table1), and the matched records from the right table (table2)
RIGHT (OUTER) JOIN: returns all records from the right table
(table2), and the matched records from the left table (table1)
5. View Queries
CREATE: create a view