SQL Cheat Sheet: Fundamentals SQL Intermediate:: Performing Calculations With SQL Joins & Complex Queries
SQL Cheat Sheet: Fundamentals SQL Intermediate:: Performing Calculations With SQL Joins & Complex Queries
SQL Cheat Sheet: Fundamentals SQL Intermediate:: Performing Calculations With SQL Joins & Complex Queries
Remember: The order of clauses matters in SQL. SQL Joining tables using a RIGHT JOIN:
uses the following order of precedence: FROM, SELECT,
SELECT f.name country, c.name city
LIMIT. FROM cities c
RIGHT JOIN facts f ON f.id = c.facts;
Display the whole table:
SELECT * Joining tables using a FULL OUTER JOIN:
FROM table_name;
SELECT f.name country, c.name city
FROM cities c
Select specific columns from a table:
FULL OUTER JOIN facts f ON f.id = c.facts_id;
SELECT column_name_1, column_name_2
FROM table_name;
Sorting a column without specifying a column name:
Display the first 10 rows on a table: SELECT name, migration_rate FROM FACTS
ORDER BY 2 desc; -- 2 refers to migration_rate column
SELECT *
FROM table_name;
LIMIT 10; Using a join within a subquery, with a limit:
SELECT c.name capital_city, f.name country
FROM facts f
Adding comments to your SQL queries INNER JOIN (
SELECT * FROM cities
WHERE capital = 1
) c ON c.facts_id = f.id
Adding single-line comments:
INNER 10
-- First comment
SELECT column_1, column_2, column_3 -- Second comment
FROM table_name; -- Third comment Joining data from more than two tables:
SELECT [column_names] FROM [table_name_one]
Adding block comments: [join_type] JOIN [table_name_two] ON [join_constraint]
[join_type] JOIN [table_name_three] ON [join_constraint]
/* ...
This comment ...
spans over ...
multiple lines [join_type] JOIN [table_name_three] ON [join_constraint]
*/
SELECT column_1, column_2, column_3
FROM table_name;
Other common SQL operations:
Reserved words are words that cannot be used as identifiers (such as variable names or function names) in a programming
language, because they have a specific meaning in the language itself. Here is a list of reserved words in SQL.