πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

SQL Cheatsheet β€” Quick Reference for Beginners and Professionals

P
php Guru
Β· November 24, 2025 Β· 4 min read Β· Updated November 24, 2025
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

πŸ“Œ Key Takeaways

  • SQL Cheatsheet β€” Quick Reference for Beginners and Professionals
  • SQL Basics for Beginners
  • SQL Data Types
  • Inserting and Updating Data in SQL
  • SQL SELECT Statement Explained
  • SQL WHERE Clause
Advertisement

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)
P
php Guru
← Previous Post
C# Cheat Sheet β€” Complete Syntax, OOP Concepts, and Examples
Next Post β†’
MongoDB Cheatsheet β€” Complete MongoDB Commands, Queries & Aggregation Guide

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 10   +   8   =