Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL Cheatsheet

This document is a comprehensive SQL cheatsheet covering essential commands, aggregate functions, grouping, joins, subqueries, window functions, constraints, indexing, and transactions. It provides examples for each topic, highlighting key SQL operations and best practices for interviews. Quick tips emphasize the importance of mastering joins, window functions, and understanding data integrity concepts.

Uploaded by

rohitsul1112003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Cheatsheet

This document is a comprehensive SQL cheatsheet covering essential commands, aggregate functions, grouping, joins, subqueries, window functions, constraints, indexing, and transactions. It provides examples for each topic, highlighting key SQL operations and best practices for interviews. Quick tips emphasize the importance of mastering joins, window functions, and understanding data integrity concepts.

Uploaded by

rohitsul1112003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Complete SQL Cheatsheet for

Interviews & Practice


1. Basic SQL Commands
●​ SELECT: Retrieves data from a table.
●​ SELECT DISTINCT: Returns only unique (distinct) values.
●​ WHERE: Filters records based on a condition.
●​ ORDER BY: Sorts the result in ascending (ASC) or descending (DESC) order.
●​ LIMIT: Limits the number of rows returned.

SELECT name, age FROM employees WHERE age > 30 ORDER BY age DESC LIMIT 5;

2. Aggregate Functions
●​ COUNT(): Counts the number of rows.
●​ SUM(): Adds up values.
●​ AVG(): Calculates average value.
●​ MAX()/MIN(): Finds highest/lowest value.

SELECT COUNT(*), AVG(salary), MAX(age) FROM employees;

3. Grouping & Filtering


●​ GROUP BY:Groups rows sharing a property for aggregate calculations.
●​ HAVING: Filters groups (like WHERE but for grouped data).

SELECT department, AVG(salary) FROM employees GROUP BY department HAVING


AVG(salary) > 50000;

4. Joins (Combining Tables)


●​ INNER JOIN: Returns records that have matching values in both tables.
●​ LEFT JOIN: All records from left + matched from right.
●​ RIGHT JOIN: All records from right + matched from left.
●​ FULL OUTER JOIN: All records when there is a match in either table.
SELECT a.name, b.project FROM employees a LEFT JOIN projects b ON a.id = b.emp_id;

5. Subqueries & CTE (Common Table Expressions)


●​ Subquery: Query inside another query.
●​ CTE: Temporary result set for readable, reusable queries (using WITH).

WITH HighEarners AS (SELECT name, salary FROM employees WHERE salary > 80000)
SELECT * FROM HighEarners;

6. Window Functions
●​ Perform calculations across rows related to current row (without GROUP BY).
●​ RANK(): Rank of rows.
●​ LAG()/LEAD(): Access previous/next row data.

SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM employees;

7. Constraints
Rules applied to table columns to enforce data integrity:

●​ NOT NULL: No empty values.


●​ UNIQUE: No duplicate values.
●​ PRIMARY KEY: Unique & NOT NULL.
●​ FOREIGN KEY: Link to another table.
●​ CHECK: Condition to validate data.
●​ DEFAULT: Assign default value.

CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, salary
DECIMAL(10,2) CHECK (salary > 0));

8. Indexing for Performance


Indexes speed up data retrieval but slow down inserts/updates.

●​ CREATE INDEX: Make an index.


●​ DROP INDEX: Remove an index.

CREATE INDEX idx_name ON employees(name);

9. Transactions
●​ Set of SQL operations that are atomic (all succeed or all fail).
●​ BEGIN TRANSACTION: Start.
●​ COMMIT: Save changes.
●​ ROLLBACK: Undo changes if error occurs.

BEGIN TRANSACTION;

UPDATE accounts SET balance = balance - 500 WHERE id = 1;

UPDATE accounts SET balance = balance + 500 WHERE id = 2;

COMMIT;

Quick Tips for Interviews


●​ Master Joins and Window Functions — frequently asked.
●​ Avoid SELECT * — select only needed columns.
●​ Use Indexes for faster queries.
●​ Understand Primary & Foreign Keys, Normalization.

You might also like