sql cheatsheet, sql tutorial, sql queries with examples, sql commands list, sql joins explained, sql functions, sql interview questions, database query guide, mysql cheatsheet, sql for beginners
sql cheatsheet, sql tutorial, sql queries with examples, sql commands list, sql joins explained, sql functions, sql interview questions, database query guide, mysql cheatsheet, sql for beginners

SQL Cheatsheet — Quick Reference for Beginners and Professionals

Structured Query Language (SQL) is the standard language used for managing and manipulating databases.
This SQL Cheatsheet is your complete quick reference for creating, updating, and managing data — whether you use MySQL, SQL Server, PostgreSQL, or Oracle.


SQL Basics for Beginners

CommandPurposeExample
CREATE DATABASECreates a new databaseCREATE DATABASE company;
USESelects a databaseUSE company;
CREATE TABLECreates a new tableCREATE TABLE employees (id INT, name VARCHAR(50));
DROP TABLEDeletes a tableDROP TABLE employees;
ALTER TABLEModifies a table structureALTER TABLE employees ADD salary INT;

SQL Data Types

CategoryData TypesExample
NumericINT, FLOAT, DECIMALsalary DECIMAL(10,2)
StringCHAR, VARCHAR, TEXTname VARCHAR(100)
Date/TimeDATE, TIME, DATETIME, TIMESTAMPcreated_at DATETIME
BooleanBOOLEAN, BITis_active BOOLEAN

Inserting and Updating Data in SQL

Insert Data into a Table

INSERT INTO employees (id, name, salary)
VALUES (1, 'John Doe', 60000);

Update Existing Records

UPDATE employees
SET salary = 70000
WHERE id = 1;

Delete a Record

DELETE FROM employees
WHERE id = 1;

SQL SELECT Statement Explained

The SELECT statement is used to retrieve data from one or more tables.

SELECT name, salary FROM employees;

Aliases Example:

SELECT name AS EmployeeName, salary AS MonthlySalary FROM employees;

SQL WHERE Clause

Used to filter records based on a condition.

SELECT * FROM employees WHERE salary > 50000;

Common Operators:
=, !=, >, <, >=, <=, BETWEEN, IN, LIKE, IS NULL

Example:

SELECT * FROM employees WHERE name LIKE 'J%';

SQL ORDER BY Clause

Sorts the result in ascending (ASC) or descending (DESC) order.

SELECT * FROM employees ORDER BY salary DESC;

SQL GROUP BY and HAVING Clauses

GROUP BY groups similar data.
HAVING filters aggregated data.

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

SQL Joins — Combining Data from Multiple Tables

TypeDescriptionExample
INNER JOINReturns matching records from both tablesSELECT * FROM employees e INNER JOIN departments d ON e.dept_id = d.id;
LEFT JOINAll records from left + matched from rightSELECT * FROM employees e LEFT JOIN departments d ON e.dept_id = d.id;
RIGHT JOINAll records from right + matched from leftSELECT * FROM employees e RIGHT JOIN departments d ON e.dept_id = d.id;
FULL JOINAll records when there is a matchSELECT * FROM employees e FULL JOIN departments d ON e.dept_id = d.id;

SQL Aggregate Functions

FunctionDescriptionExample
COUNT()Returns number of rowsSELECT COUNT(*) FROM employees;
SUM()Returns total sumSELECT SUM(salary) FROM employees;
AVG()Returns average valueSELECT AVG(salary) FROM employees;
MIN()Returns minimum valueSELECT MIN(salary) FROM employees;
MAX()Returns maximum valueSELECT MAX(salary) FROM employees;
sql cheatsheet, sql tutorial, sql queries with examples, sql commands list, sql joins explained, sql functions, sql interview questions, database query guide, mysql cheatsheet, sql for beginners

SQL Constraints

ConstraintPurposeExample
PRIMARY KEYUniquely identifies each recordid INT PRIMARY KEY
FOREIGN KEYLinks to another tableFOREIGN KEY (dept_id) REFERENCES departments(id)
NOT NULLEnsures column cannot be nullname VARCHAR(50) NOT NULL
UNIQUEPrevents duplicate valuesemail VARCHAR(100) UNIQUE
CHECKValidates dataCHECK (salary > 0)
DEFAULTSets default valuestatus VARCHAR(10) DEFAULT 'Active'

SQL Subqueries

A subquery is a query inside another query.

SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

SQL Views

A view is a virtual table.

CREATE VIEW high_salary AS
SELECT name, salary FROM employees WHERE salary > 70000;

SQL Indexes

Indexes improve query performance.

CREATE INDEX idx_name ON employees(name);

SQL Transactions

Used for safe execution of multiple statements.

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

Use ROLLBACK; to undo changes.


SQL Interview Questions

Q1. What is the difference between WHERE and HAVING?
➡️ WHERE filters rows before grouping, HAVING filters after grouping.

Q2. What is normalization?
➡️ The process of organizing data to reduce redundancy.

Q3. What is a foreign key?
➡️ It’s a constraint that creates a relationship between two tables.

Q4. What is the difference between INNER JOIN and OUTER JOIN?
➡️ INNER JOIN returns matched rows only, while OUTER JOIN returns all rows including unmatched ones.


FAQ — SQL Cheatsheet

Q1: What is SQL used for?
SQL is used to store, retrieve, and manipulate data in relational databases.

Q2: Which databases use SQL?
MySQL, PostgreSQL, SQL Server, Oracle, and SQLite all use SQL syntax.

Q3: Is SQL case-sensitive?
SQL keywords are not case-sensitive, but string comparisons may be.

Q4: What are DDL, DML, and DCL commands?

  • DDL: Data Definition Language (CREATE, ALTER, DROP)
  • DML: Data Manipulation Language (INSERT, UPDATE, DELETE)
  • DCL: Data Control Language (GRANT, REVOKE)
Related Article
Machine Learning Cheatsheet (Unsupervised & Reinforcement Learning)

Machine Learning (ML) is a crucial part of artificial intelligence, enabling systems to automatically learn from data.This Machine Learning Cheatsheet Read more

HTML Cheat Sheet — Reference Guide to HTML Tags, Attributes, and Examples

HTML cheat sheet, HTML tags reference, HTML attributes list, HTML examples for beginners, semantic HTML guide, HTML forms tutorial, HTML Read more

Python Cheat Sheet — Complete Syntax Reference and Programming Examples

This Python Cheat Sheet is your quick reference guide for writing efficient Python code. Whether you’re preparing for coding interviews, Read more

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples This PHP Cheat Sheet serves as a quick, structured reference for Read more

JavaScript Cheat Sheet — Complete ES6 Syntax, Functions, and DOM Methods with Examples

JavaScript Cheat Sheet — Complete Syntax Reference & Examples JavaScript is the core scripting language of the web, enabling interactivity, Read more

CSS Cheat Sheet — Complete CSS3 Selectors, Properties, and Layout Examples

CSS Cheat Sheet — Complete CSS3 Syntax, Selectors & Layout Examples Cascading Style Sheets (CSS) is the language used to Read more

Java Cheat Sheet — Complete Java Syntax, Data Types, Loops, and OOP Concepts for Beginners

Java Cheat Sheet — Complete Java Syntax, Classes, and Examples Java is a powerful, object-oriented programming language widely used for Read more

HTML5 Cheat Sheet — Complete Tag Reference & Examples

HTML5 is the core markup language of the modern web, used to structure content such as text, images, forms, and Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments