SQL (Structured Query Language): the standard programming language for managing relational databases.
RDBMS: Relational Database Management System
Common databases (by type): MySQL, PostgreSQL, SQLite, Oracle, SQL Server
SQLite: lightweight, embedded — great for mobile apps
MySQL: open-source, widely used — great for web apps
PostgreSQL: open-source, feature-rich — great for complex apps
Oracle: enterprise-grade, fully featured — great for large-scale apps
SQL Server: developed by Microsoft — great for Windows environments
1.2 Basic SQL Categories
Four schools of thought — these are the disciplines you use to communicate with a database. Master them and you’re a data wrangler; give up and you’re just a data janitor. 🐶
Category
Purpose
Keywords
DDL
Define database structure
CREATE, ALTER, DROP
DML
Manipulate data
INSERT, UPDATE, DELETE
DQL
Query data
SELECT
DCL
Control permissions
GRANT, REVOKE
II. Basic Syntax
2.1 Basic Rules
SQL statements end with a semicolon ; (some databases allow omitting it)
Keywords are case-insensitive, but the convention is to write keywords in uppercase and table/column names in lowercase
Strings and dates are wrapped in single quotes ' '
-- Recommended writing style SELECT id, name, email FROM users /* Whether to use double quotes depends on the database type */ WHERE status ='active' ORDERBY create_time DESC;
2.3 Common Operators
Arithmetic Operators
Operator
Description
+
Addition
-
Subtraction
*
Multiplication
/
Division
% or MOD()
Modulo
Comparison Operators
Operator
Description
=
Equal to
<> or !=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
Logical Operators
Operator
Description
AND
Logical AND (higher precedence than OR — use parentheses like in C++)
OR
Logical OR
NOT
Logical NOT
2.4 Common Commands (MySQL)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
-- Show all databases SHOW DATABASES;
-- Show all tables in the current database SHOW TABLES;
-- View table structure DESC table_name; -- or DESCRIBE table_name;
-- View the CREATE TABLE statement SHOWCREATE TABLE table_name;
-- Show full column info for a table SHOWFULL COLUMNS FROM table_name;
2.5 ⚠️ Things to Watch Out For
Query syntax keywords have a specific ordering relationship.
-- Comparison operators WHERE age >18 WHERE name ='Alice' WHERE age >=18AND age <=30
-- Range WHERE age BETWEEN18AND30
-- Enumeration WHERE status IN ('active', 'pending')
-- Pattern matching WHERE name LIKE'A%'-- starts with A WHERE name LIKE'%son%'-- contains "son" WHERE name LIKE'A_'-- starts with A, exactly 2 characters
-- Null checks WHERE email ISNULL WHERE email ISNOT NULL
-- MySQL SELECT*FROM table_name LIMIT 10; SELECT*FROM table_name LIMIT 5, 10; -- Start from row 5, fetch 10 rows
-- SQL Server SELECT TOP 10*FROM table_name;
-- Oracle SELECT*FROM table_name WHERE ROWNUM <=10;
4.5 Aggregate Functions
1 2 3 4 5 6 7 8
SELECT COUNT(*) AS total_rows, COUNT(col) AS non_null_count, SUM(col) AS total, AVG(col) AS average, MAX(col) AS maximum, MIN(col) AS minimum FROM table_name;
4.6 Grouping — GROUP BY
1 2 3 4
SELECT col, aggregate_function FROM table_name GROUPBY col HAVING aggregate_condition;
Note: WHERE filters before grouping; HAVING filters after grouping.
1 2 3 4 5
-- Example: average salary per department SELECT dept_id, AVG(salary) AS avg_salary FROM employees GROUPBY dept_id HAVINGAVG(salary) >5000;
4.7 Multi-Table Queries
Joins (JOIN)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
-- INNER JOIN: only keep matching rows SELECT* FROM table1 INNERJOIN table2 ON table1.col = table2.col;
-- LEFT JOIN: keep all rows from the left table; NULLs where there's no match on the right SELECT* FROM table1 LEFTJOIN table2 ON table1.col = table2.col;
-- RIGHT JOIN: keep all rows from the right table SELECT* FROM table1 RIGHTJOIN table2 ON table1.col = table2.col;
-- FULL JOIN (MySQL doesn't support this natively — simulate with UNION) SELECT*FROM table1 LEFTJOIN table2 ON ... UNION SELECT*FROM table1 RIGHTJOIN table2 ON ...;
Subqueries
1 2 3 4 5 6 7 8
-- Subquery in WHERE SELECT*FROM table_name WHERE col = (SELECT col FROM ...);
-- IN subquery SELECT*FROM table_name WHERE col IN (SELECT col FROM ...);