PostgreSQL Mastery Cheat Sheet
PostgreSQL Mastery Cheat Sheet
io
1
SELECT * 1
SELECT COUNT(*)
1
SELECT first_name 1
SELECT SUM(sale_amount)
1
SELECT *
1
SELECT MAX(salary)
2
FROM user_details
2 FROM salaries;
3 WHERE first_name = 'john';
2 FROM exam_scores;
1
SELECT product_name
2
FROM products
Retrieve all employees whose age is between 25 and 40 from the employees 2 FROM orders;
table.
1
SELECT *
2
FROM employees
1
SELECT *
1
SELECT country, COUNT(*)
2
FROM customers
2
FROM users
1
SELECT DISTINCT department
1
SELECT category, SUM(sales)
2
FROM departments;
2
FROM product_sales
3 GROUP BY category;
1
SELECT department, SUM(salary)
Inner Join 2
FROM salaries
2
FROM employees e
3
INNER JOIN departments d
4 ON e.department_id = d.department_id;
5. Subqueries
Left Join
Subquery in WHERE
Get all employees and their assigned projects, even if some employees don’t have
projects. Fetch all products with a price greater than the average price.
1
SELECT e.first_name, p.project_name
1
SELECT *
2
FROM employees e
2
FROM products
3
LEFT JOIN projects p
3 WHERE price > (SELECT AVG(price) FROM products);
4 ON e.employee_id = p.employee_id;
List all projects and the employees assigned to them, including projects without Retrieve employee names and their corresponding department names using a
employees. subquery.
1
SELECT p.project_name, e.first_name
1
SELECT first_name,
2
FROM projects p
2
(SELECT department_name
3
RIGHT JOIN employees e
3
FROM departments
4 ON p.project_id = e.project_id; 4
WHERE department_id = employees.department_id) AS
5
department
6 FROM employees;
Full Outer Join
Retrieve all employees and all projects, including those with no matching records.
Subquery in FROM
1
SELECT e.first_name, p.project_name
Find the total sales per product using a subquery.
2
FROM employees e
3
FULL OUTER JOIN projects p
1
SELECT department, SUM(salary)
4 ON e.employee_id = p.employee_id; 2
FROM salaries
3
GROUP BY department
6. Window Functions
Rank Over Partition 7. String Functions
Rank products based on sales within each category.
Concatenate Strings
1
SELECT product_id, category_id, sales_amount,
4 FROM products; 2
FROM users;
Running Total
Calculate the cumulative sales for each product over time. String Length
Get the length of the product name.
1
SELECT product_id, sale_date, sales_amount,
2
SUM(sales_amount) OVER (ORDER BY sale_date) AS 1
SELECT LENGTH(product_name)
3
running_total
2
FROM products;
4 FROM sales;
Lag Function
Substring
Find the previous order amount for each user.
Extract the first 3 characters of the product name.
1
SELECT user_id, order_date, order_amount,
2
1
SELECT SUBSTRING(product_name, 1, 3)
4 FROM orders;
1
SELECT UPPER(customer_name)
2
FROM customers;
Current Date
Retrieve all users who joined today.
1
SELECT *
2
FROM users
Case Statements
Extract Year
Display gender as 'M' or 'F' based on the gender column.
Get the year from the join_date column.
1
SELECT first_name, last_name,
1
SELECT EXTRACT(YEAR FROM join_date)
2
CASE
2
FROM users; 3
WHEN gender = 'male' THEN 'M'
4
WHEN gender = 'female' THEN 'F'
5
END AS gender_abbr
6 FROM users;
Date Difference
Calculate the number of days between order_date and today.
Coalesce Function
1
SELECT age(CURRENT_DATE, order_date)
Replace null values in the address column with 'unknown'.
2
FROM orders;
1
SELECT COALESCE(address, 'unknown')
2
FROM customers;
2
FROM orders;
Recursive Query
1
WITH numbers AS (
2
SELECT 1 AS number
3
UNION ALL
4
SELECT number + 1
5
FROM numbers
6
WHERE number < 10
7
)
8
SELECT number
9
FROM numbers;
1
SELECT e.first_name, p.project_name
2
FROM employees e
1
SELECT e.first_name, p.project_name
2
FROM employees e
3
CROSS JOIN projects p