Download SQL Cheat Sheet: Selecting Tables, Columns, and Rows and more Slides Database Programming in PDF only on Docsity! Selecting tables, columns, and rows: SQL Cheat Sheet: Fundamentals Performing calculations with SQL Display the whole table: SELECT FROM * table_name; Performing a single calculation: SELECT 1320+17; Performing multiple calculations: SELECT 1320+17, 1340-3, 7*191, 8022/6; Performing calculations with multiple numbers: SELECT 1*2*3, 1+2+3; Renaming results: SELECT 2*3 AS mult, 1+2+3 AS nice_sum; SELECT FROM Remember: The order of clauses matters in SQL. SQL uses the following order of precedence: FROM, SELECT, LIMIT. column_name_1, column_name_2 table_name; Display the first 10 rows on a table: SELECT FROM LIMIT * table_name; 10; Adding comments to your SQL queries Adding single-line comments: SELECT FROM -- First comment column_1, column_2, column_3 -- Second comment table_name; -- Third comment Adding block comments: SELECT FROM /* This comment spans over multiple lines */ column_1, column_2, column_3 table_name; Select specific columns from a table: Many of these examples use table and column names from the real SQL databases that learners work with in our interactive SQL courses. For more information, sign up for a free account and try one out! SELECT column_name_1, column_name_2 FROM table_name_1 INNER JOIN table_name_2 ON table_name_1.column_name_1 = table_name_2.column_name_1; Joining data in SQL: SQL Intermediate: Joins & Complex Queries Joining tables with INNER JOIN: SELECT * FROM facts LEFT JOIN cities ON cities.facts_id = facts.id; Joining tables using a LEFT JOIN: SELECT f.name country, c.name city FROM cities c RIGHT JOIN facts f ON f.id = c.facts; Joining tables using a RIGHT JOIN: SELECT f.name country, c.name city FROM cities c FULL OUTER JOIN facts f ON f.id = c.facts_id; Joining tables using a FULL OUTER JOIN: SELECT name, migration_rate FROM FACTS ORDER BY 2 desc; -- 2 refers to migration_rate column Sorting a column without specifying a column name: SELECT c.name capital_city, f.name country FROM facts f INNER JOIN ( SELECT * FROM cities WHERE capital = 1 ) c ON c.facts_id = f.id INNER 10 Using a join within a subquery, with a limit: SELECT [column_names] FROM [table_name_one] [join_type] JOIN [table_name_two] ON [join_constraint] [join_type] JOIN [table_name_three] ON [join_constraint] ... ... ... [join_type] JOIN [table_name_three] ON [join_constraint] Joining data from more than two tables: