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

SQL Query Cheat Sheet

This document is a comprehensive SQL query cheat sheet organized alphabetically, detailing various SQL commands and their meanings. It includes examples for commands such as SELECT, INSERT, UPDATE, DELETE, and various functions like AVG(), COUNT(), and SUM(). The cheat sheet serves as a quick reference for SQL syntax and usage.
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)
6 views3 pages

SQL Query Cheat Sheet

This document is a comprehensive SQL query cheat sheet organized alphabetically, detailing various SQL commands and their meanings. It includes examples for commands such as SELECT, INSERT, UPDATE, DELETE, and various functions like AVG(), COUNT(), and SUM(). The cheat sheet serves as a quick reference for SQL syntax and usage.
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

Complete SQL Query Cheat Sheet (Alphabetical)

Command Meaning Example

ALTER TABLE Modify structure (add/drop columns, etc.) ALTER TABLE users ADD email VARCHAR(100);

AND Logical operator (both conditions true) WHERE age > 18 AND status = 'active'

AS Rename a column or table (alias) SELECT name AS employee_name FROM users;

AVG() Calculate average SELECT AVG(salary) FROM employees;

BETWEEN Value in a range WHERE age BETWEEN 25 AND 35

BY Used in GROUP BY or ORDER BY GROUP BY department

CASE Conditional expression CASE WHEN salary > 50000 THEN 'High' ELSE 'Low' END

CAST() Convert a value to another data type CAST(salary AS DECIMAL(10,2))

COUNT() Count rows or values SELECT COUNT(*) FROM orders

CREATE Create a new table, view, or database CREATE TABLE users (...)

DATE_ADD() Add days to date DATE_ADD(NOW(), INTERVAL 5 DAY)

DATE_SUB() Subtract days from date DATE_SUB(NOW(), INTERVAL 5 DAY)

DATEDIFF() Difference between two dates DATEDIFF('2025-12-31', '2025-01-01')

DELETE Delete rows from a table DELETE FROM users WHERE inactive = 1;

DESC Descending order ORDER BY salary DESC

DISTINCT Remove duplicate results SELECT DISTINCT department FROM employees;

DROP Delete a table, column, or database DROP TABLE temp_data;

EXISTS Checks if subquery returns any row WHERE EXISTS (SELECT * FROM orders ...)

FROM Specifies the source table SELECT * FROM users

FULL OUTER JOIN All rows from both tables ... FULL OUTER JOIN departments d ON e.id = d.emp_id

GETDATE() Returns current date/time (SQL Server) SELECT GETDATE();

GROUP BY Groups rows for aggregation GROUP BY department_id

HAVING Filter after grouping HAVING COUNT(*) > 5

IN Check value in list WHERE country IN ('India', 'US')

INDEX Speeds up data access CREATE INDEX idx_name ON users(name);


INNER JOIN Returns matched rows from both tables ... INNER JOIN departments ON ...

INSERT INTO Adds new row(s) INSERT INTO users(name, age) VALUES ('Raj', 30);

INTERSECT Returns rows common to two queries SELECT id FROM A INTERSECT SELECT id FROM B

IS NULL Checks for nulls WHERE email IS NULL

IS NOT NULL Checks for non-nulls WHERE email IS NOT NULL

JOIN Combine rows from two or more tables SELECT * FROM A JOIN B ON A.id = B.id;

LEFT JOIN All rows from left table, matched from right ... LEFT JOIN orders ON users.id = orders.user_id

LIKE Pattern matching WHERE name LIKE 'R%'

LIMIT Restrict number of returned rows SELECT * FROM users LIMIT 10;

MAX() Returns the largest value SELECT MAX(salary) FROM employees;

MIN() Returns the smallest value SELECT MIN(join_date) FROM users;

NOT Logical NOT WHERE NOT status = 'inactive'

NOT IN Value not in a list WHERE department NOT IN ('HR', 'Sales')

NOW() Returns current date and time SELECT NOW();

NULL Missing or undefined value WHERE middle_name IS NULL

ON Specifies join condition JOIN B ON A.id = B.a_id

OR Logical OR WHERE city = 'Delhi' OR city = 'Mumbai'

ORDER BY Sorts result set ORDER BY name ASC

PRIMARY KEY Uniquely identifies each record id INT PRIMARY KEY

RANK() Ranking function with gaps RANK() OVER (PARTITION BY dept ORDER BY salary DES

RIGHT JOIN All from right, matched from left ... RIGHT JOIN dept ON emp.dept_id = dept.id

ROW_NUMBER() Unique number for each row ROW_NUMBER() OVER (ORDER BY hire_date)

SELECT Fetch data from a table SELECT name FROM users;

SET Assign new values in UPDATE SET name = 'Radha'

SUM() Adds values SELECT SUM(amount) FROM payments;

TABLE Used in FROM clause or to define a table CREATE TABLE ...

TOP Limits rows (SQL Server) SELECT TOP 5 * FROM users;


TRUNCATE Deletes all rows but keeps structure TRUNCATE TABLE logs;

UNION Combines result sets (no duplicates) SELECT id FROM A UNION SELECT id FROM B

UNION ALL Combines all rows, includes duplicates SELECT id FROM A UNION ALL SELECT id FROM B

UNIQUE No duplicate values allowed email VARCHAR(100) UNIQUE

UPDATE Modifies existing rows UPDATE users SET age = 28 WHERE id = 5;

USE Selects a database USE hr_database;

VALUES Used with INSERT to specify values VALUES ('Alice', 30)

VIEW Virtual table created from a query CREATE VIEW active_users AS SELECT * FROM users WH

WHERE Filters rows SELECT * FROM users WHERE age > 25;

You might also like