SQL-cheatsheet
SQL-cheatsheet
com
SAMPLE DATA AGGREGATION AND GROUPING
customers Table: orders
GROUP BY groups together rows with the same value in specified columns, and then
id name age city country has_subscription order_id cus_id cost computes summaries (aggregates) for each group of values.
Current Row
Fetch all columns from the customers table: Find the discount percentage for all orders: AGGREGATE RANKING VALUE
AVG() ROW_NUMBER() LAG()
SELECT *
SELECT discount/cost * 100 MAX() RANK() LEAD()
FROM customer;
FROM orders; MIN() DESNE_RANK() FIRST_VALUE() Unbounded
Fetch name and age columns for all customers: Round the discount percentage to 2 decimal places: SUM() PERCENT_RANK() LAST_VALUE() Following
COUNT() NTILE() NTH_VALUE()
SELECT name, age SELECT ROUND(discount/cost * 100, 2)
FROM customers; FROM orders;
PARTITION BY ORDER BY
Sort Output Using ORDER BY Aliases
Divides rows into multiple groups, called partitions, to Specifies the order of rows in each partition to which
Sort customers by age in the default ASCending order: AS is used to rename columns: which the window function is applied. the window function is applied.
SELECT * SELECT cost * 0.04 AS sales_tax SELECT order_id, cus_id, cost,
FROM customers SELECT order_id, cus_id, cost, SUM(cost) OVER (PARTITION BY cus_Id ORDER BY cost ASC)
FROM orders; SUM(cost) OVER (PARTITION BY cus_id) AS sum_cost
ORDER BY age ASC; AS sum_cost
FROM orders; FROM orders;
Sort customers by age in DESCending order (high to low): AS is also used to rename tables:
order_id cus_id cost sum_cost order_id cus_id cost sum_cost
SELECT * SELECT cus.name, cus.age
FROM customers FROM customers AS cus; 107 1 200.00 376.25 109 1 75.50 376.25
ORDER BY age DESC;
109 1 75.50 376.25 111 1 100.75 376.25
FILTERING OUTPUT WITH WHERE 111 1 100.75 376.25 107 1 200.00 376.25
CASE STATEMENTS
Bella 2023-10-02 400.00
SELECT orders.order_id, customers.name 3 Emma Stone USA 3 Justin Bieber Canada
Bella 2024-11-19 100.00 FROM orders
RIGHT JOIN customers id name country
Chetan NULL NULL
ON orders.cus_id = customers.id;
CASE goes through a list of conditions and returns a SELECT name
value when the first condition is met. FROM actors
1 Ryan Gosling Canada
WHERE country = ‘Canada’
CROSS JOIN SELF JOIN If no conditions are true it returns the value in the UNION ALL 2 Drake Canada
ELSE clause. SELECT name
CROSS JOIN returns all possible combinations or rows SELF JOIN is used to join a table with itself to compare 1 Drake Canada
SELECT order_id, cus_id, cost, FROM singers
from both tables. It doesn’t have a join condition! rows within the same table. It’s typically used with an alias WHERE country = ‘Canada’;
CASE 3 Justin Bieber Canada
to differentiate the table instances. WHEN cost > 175 THEN 'luxury'
SELECT orders.order_id, customers.name
SELECT A.cus_id, A.order_id AS ord_1, A.date AS WHEN cost > 100 THEN 'mid-tier'
FROM orders Practice real Amazon UNION SQL Interview Question:
CROSS JOIN customers; date_1, B.order_id AS ord_2, B.date AS date_2 ELSE 'budget' END AS product_type
FROM orders; datalemur.com/questions/prime-warehouse-storage
FROM orders A
JOIN orders B ON A.cus_id = B.cus_id