SQL Quick Reference Guide

1. Creating a Table

Syntax

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype
);

Example

CREATE TABLE Student (
    student_id INT PRIMARY KEY,
    name VARCHAR(100),
    age INT
);

2. Inserting into a Table

Insert a single row

INSERT INTO Student (student_id, name, age)
VALUES (1, 'Rahul', 20);

Insert multiple rows

INSERT INTO Student (student_id, name, age)
VALUES
    (2, 'Priya', 21),
    (3, 'Arun', 19);

3. Basic SELECT Commands

Select all columns

SELECT * FROM Student;

Select specific columns

SELECT name, age
FROM Student;

Filter records

SELECT *
FROM Student
WHERE age > 20;

Sort records

SELECT *
FROM Student
ORDER BY age DESC;

Remove duplicate results

SELECT DISTINCT age
FROM Student;

Limit the number of rows (MySQL)

SELECT *
FROM Student
LIMIT 5;

Aggregate functions

SELECT COUNT(*) FROM Student;
SELECT AVG(age) FROM Student;
SELECT MAX(age) FROM Student;
SELECT MIN(age) FROM Student;
SELECT SUM(age) FROM Student;

Group records

SELECT age, COUNT(*)
FROM Student
GROUP BY age;

Filter groups

SELECT age, COUNT(*)
FROM Student
GROUP BY age
HAVING COUNT(*) > 1;

Update records

UPDATE Student
SET age = 22
WHERE student_id = 1;

Delete records

DELETE FROM Student
WHERE student_id = 1;

4. Triggers — Syntax Only (MySQL)

DELIMITER //

CREATE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
    -- SQL statements
END //

DELIMITER ;

Trigger events: INSERT, UPDATE, DELETE

Trigger timings: BEFORE, AFTER

5. Stored Procedures — Syntax Only (MySQL)

DELIMITER //

CREATE PROCEDURE procedure_name(
    IN parameter_name INT
)
BEGIN
    -- SQL statements
END //

DELIMITER ;

Calling a procedure

CALL procedure_name(10);

6. Views — Syntax Only

Create a view

CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;

Use a view

SELECT * FROM view_name;

Modify a view

CREATE OR REPLACE VIEW view_name AS
SELECT column1
FROM table_name;

Drop a view

DROP VIEW view_name;

7. Joins

Assume two tables: Student(student_id, name) and Enrollment(student_id, course_id).

INNER JOIN — Returns matching rows from both tables.

SELECT Student.name, Enrollment.course_id
FROM Student
INNER JOIN Enrollment
ON Student.student_id = Enrollment.student_id;

LEFT JOIN — Returns all rows from the left table and matching rows from the right.

SELECT *
FROM Student
LEFT JOIN Enrollment
ON Student.student_id = Enrollment.student_id;

RIGHT JOIN — Returns all rows from the right table and matching rows from the left.

SELECT *
FROM Student
RIGHT JOIN Enrollment
ON Student.student_id = Enrollment.student_id;

CROSS JOIN — Returns every possible combination of rows.

SELECT *
FROM Student
CROSS JOIN Enrollment;

SELF JOIN — Joins a table with itself.

SELECT A.name, B.name
FROM Student A
JOIN Student B
ON A.student_id < B.student_id;

8. Set Operations

Set operations combine the results of two SELECT statements. Both queries must return the same number of columns with compatible data types.

UNION — Combines results and removes duplicates.

SELECT name FROM Student
UNION
SELECT name FROM Alumni;

UNION ALL — Combines results and retains duplicates.

SELECT name FROM Student
UNION ALL
SELECT name FROM Alumni;

INTERSECT — Returns rows common to both results.

SELECT name FROM Student
INTERSECT
SELECT name FROM Alumni;

EXCEPT — Returns rows from the first result that are absent from the second.

SELECT name FROM Student
EXCEPT
SELECT name FROM Alumni;