

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This sql cheat sheet covers the fundamentals of selecting tables, columns, and rows using sql. Topics include performing calculations, displaying specific columns, joining tables, and using comments. Examples use various sql statements and clauses.
Typology: Slides
1 / 2
This page cannot be seen from the preview
Don't miss anything!


table_name;
SELECT 2*3 AS mult, 1+2+3 AS nice_sum;
Remember: The order of clauses matters in SQL. SQL
uses the following order of precedence: FROM, SELECT,
LIMIT.
column_name_1, column_name_ table_name;
table_name; 10 ;
-- First comment column_1, column_2, column_3 -- Second comment table_name; -- Third comment
This comment spans over multiple lines */ column_1, column_2, column_ table_name;
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_ INNER JOIN table_name_2 ON table_name_1.column_name_ = table_name_2.column_name_1;
SELECT * FROM facts LEFT JOIN cities ON cities.facts_id = facts.id;
SELECT f.name country, c.name city FROM cities c RIGHT JOIN facts f ON f.id = c.facts;
SELECT f.name country, c.name city FROM cities c FULL OUTER JOIN facts f ON f.id = c.facts_id;
SELECT name, migration_rate FROM FACTS ORDER BY 2 desc; -- 2 refers to migration_rate column
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
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]
album_id, artist_id, "album id is " || album_id col_1, "artist id is " || artist_id col2, album_id || artist_id col FROM album LIMIT 3;
first_name, last_name, phone FROM customer WHERE first_name LIKE "%Jen%";
WHEN [comparison_1] THEN [value_1] WHEN [comparison_2] THEN [value_2] ELSE [value_3] END AS [new_column_name]
WITH track_info AS ( SELECT t.name, ar.name artist, al.title album_name, FROM track t INNER JOIN album al ON al.album_id = t.album_id INNER JOIN artist ar ON ar.artist_id = al.artist_id ) SELECT * FROM track_info WHERE album_name = "Jagged Little Pill";
CREATE VIEW chinook.customer_2 AS SELECT * FROM chinook.customer;
[select_statement_one] UNION [select_statement_two];
SELECT * from customer_usa INTERSECT SELECT * from customer_gt_90_dollars;
SELECT * from customer_usa EXCEPT SELECT * from customer_gt_90_dollars;
DROP VIEW chinook.customer_2;
usa AS ( SELECT * FROM customer WHERE country = "USA" ), last_name_g AS ( SELECT * FROM usa WHERE last_name LIKE "G%" ), state_ca AS ( SELECT * FROM last_name_g WHERE state = "CA" ) SELECT first_name, last_name, country, state FROM state_ca