0% found this document useful (0 votes)
3 views5 pages

SQL Sheet

This document provides a comprehensive overview of SQL, including its basic concepts, database and table commands, data types, constraints, keys, and various SQL operations such as INSERT, SELECT, UPDATE, and DELETE. It also covers advanced topics like JOINs, UNION, subqueries, and the importance of clause order in SQL queries. Additionally, it categorizes SQL types into DDL, DML, DQL, DCL, and TCL, and highlights key points about 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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

SQL Sheet

This document provides a comprehensive overview of SQL, including its basic concepts, database and table commands, data types, constraints, keys, and various SQL operations such as INSERT, SELECT, UPDATE, and DELETE. It also covers advanced topics like JOINs, UNION, subqueries, and the importance of clause order in SQL queries. Additionally, it categorizes SQL types into DDL, DML, DQL, DCL, and TCL, and highlights key points about 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 DOCX, PDF, TXT or read online on Scribd

SQL COMPLETE SHEET

🔹 BASICS

 SQL → Structured Query Language

 Used for → CRUD (Create, Read, Update, Delete)

 Works with → Relational Databases (tables)

🔹 DATABASE COMMANDS

 CREATE DATABASE db_name;

 DROP DATABASE db_name;

 SHOW DATABASES;

 USE db_name;

 CREATE DATABASE IF NOT EXISTS db_name;

 DROP DATABASE IF EXISTS db_name;

🔹 TABLE COMMANDS

 CREATE TABLE table_name (col datatype constraint);

 DROP TABLE table_name;

 TRUNCATE TABLE table_name;

 SHOW TABLES;

🔹 DATA TYPES (COMMON)

 INT, TINYINT

 VARCHAR, TEXT

 DATE, TIME

 BOOLEAN

🔹 CONSTRAINTS

 NOT NULL → cannot be empty

 UNIQUE → no duplicate

 PRIMARY KEY → unique + not null

 FOREIGN KEY → link tables


 DEFAULT → default value

 CHECK → condition

🔹 KEYS

 Primary Key (PK) → unique id (1 per table)

 Foreign Key (FK) → reference another table

🔹 INSERT DATA

INSERT INTO table_name (col1, col2)


VALUES (val1, val2);

🔹 SELECT (READ)

 SELECT * FROM table;

 SELECT col1, col2 FROM table;

🔹 WHERE (FILTER)

SELECT * FROM table


WHERE condition;

Operators

 Arithmetic → + - * / %

 Comparison → = != > < >= <=

 Logical → AND OR NOT

 Special → IN, BETWEEN, LIKE, ANY, ALL

🔹 LIMIT

 LIMIT n; → restrict rows

🔹 ORDER BY

 ORDER BY col ASC;

 ORDER BY col DESC;

🔹 AGGREGATE FUNCTIONS
 COUNT()

 MAX()

 MIN()

 SUM()

 AVG()

🔹 GROUP BY

SELECT col, COUNT(*)


FROM table
GROUP BY col;

🔹 HAVING

SELECT col, COUNT(*)


FROM table
GROUP BY col
HAVING condition;

🔹 UPDATE

UPDATE table
SET col1 = val1
WHERE condition;

🔹 DELETE

DELETE FROM table


WHERE condition;

🔹 ALTER TABLE

 ADD COLUMN

 DROP COLUMN

 RENAME TO

 MODIFY COLUMN

 CHANGE COLUMN

🔹 JOINS
INNER JOIN

SELECT *
FROM A
INNER JOIN B
ON [Link] = [Link];

LEFT JOIN

 All from left + matched from right

RIGHT JOIN

 All from right + matched from left

FULL JOIN

 Left + Right (use UNION in MySQL)

SELF JOIN

 Table joins with itself

🔹 UNION

SELECT col FROM A


UNION
SELECT col FROM B;

 Removes duplicates

🔹 SUBQUERY

SELECT col
FROM table
WHERE col > (SELECT AVG(col) FROM table);

🔹 CASCADE

 ON DELETE CASCADE

 ON UPDATE CASCADE

🔹 VIEWS

 Virtual table

CREATE VIEW view_name AS


SELECT col FROM table;
🔹 CLAUSE ORDER (IMPORTANT)

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY;

🔹 SQL TYPES

 DDL → CREATE, ALTER, DROP

 DML → INSERT, UPDATE, DELETE

 DQL → SELECT

 DCL → GRANT, REVOKE

 TCL → COMMIT, ROLLBACK

🔹 IMPORTANT POINTS

 SQL is case-insensitive

 ; ends query

 Always use WHERE in UPDATE/DELETE

 PK = unique, FK = relation

You might also like