0% found this document useful (0 votes)
73 views3 pages

SQL Cheat Sheet

This document is a SQL cheat sheet that outlines essential formulas and queries for various operations. It covers basic SELECT syntax, filtering with WHERE, sorting results, using aggregate functions, GROUP BY and HAVING clauses, JOINs, CASE statements, string and date functions, and logical operators. Each section provides example queries to illustrate the concepts.

Uploaded by

barachelmboumo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views3 pages

SQL Cheat Sheet

This document is a SQL cheat sheet that outlines essential formulas and queries for various operations. It covers basic SELECT syntax, filtering with WHERE, sorting results, using aggregate functions, GROUP BY and HAVING clauses, JOINs, CASE statements, string and date functions, and logical operators. Each section provides example queries to illustrate the concepts.

Uploaded by

barachelmboumo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Cheat Sheet - Essential Formulas & Queries

A. Basic SELECT Syntax

SELECT column1, column2 FROM table_name;

SELECT * FROM table_name;

B. Filtering with WHERE

SELECT * FROM employees WHERE salary > 50000;

C. Sorting Results

SELECT * FROM students ORDER BY score DESC;

D. LIMIT Results

SELECT * FROM books LIMIT 5;

E. Aggregate Functions

SELECT COUNT(*) FROM students;

SELECT AVG(score) FROM students;

SELECT SUM(price) FROM orders;

SELECT MAX(salary) FROM employees;

SELECT MIN(age) FROM customers;

F. GROUP BY and HAVING

SELECT department, AVG(salary) AS avg_salary

FROM employees

GROUP BY department

HAVING AVG(salary) > 50000;

G. JOINs

-- Inner Join
SQL Cheat Sheet - Essential Formulas & Queries
SELECT a.name, b.class_name

FROM students a

JOIN classes b ON a.class_id = b.id;

-- Left Join

SELECT a.name, b.class_name

FROM students a

LEFT JOIN classes b ON a.class_id = b.id;

H. CASE Statement

SELECT name,

CASE

WHEN score >= 50 THEN 'PASS'

ELSE 'FAIL'

END AS result

FROM students;

I. String Functions

SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;

SELECT LENGTH(name) FROM users;

SELECT UPPER(name), LOWER(name) FROM users;

J. Date Functions

SELECT CURRENT_DATE;

SELECT NOW();

SELECT DATEDIFF(NOW(), birthdate) AS age_days FROM users;

K. Logical Operators

SELECT * FROM products

WHERE price BETWEEN 100 AND 500


SQL Cheat Sheet - Essential Formulas & Queries
AND category IN ('Laptop', 'Tablet')

AND name LIKE 'S%';

You might also like