The four families of SQL commands
Keep this chart in your head whenever you work with a database: every SQL command belongs to one of four groups. The names sound confusing at first, but each one says what it does.
CREATEALTERDROPTRUNCATEDefines the structure: databases, tables, columns, constraints. CREATE makes things, ALTER modifies them, DROP deletes them, TRUNCATE empties them.
INSERTUPDATEDELETESELECTWorks on the data inside tables, and is the most frequently used group. INSERT adds rows, UPDATE changes them, DELETE removes them, SELECT fetches them (CRUD!).
COMMITROLLBACKControls transactions. COMMIT saves changes permanently (like conn.commit() in Chapter 2); ROLLBACK undoes them if something goes wrong. A transaction either completes fully or not at all.
GRANTREVOKEControls permissions. GRANT gives a user access; REVOKE takes it back.
Some books put SELECT in its own group, DQL (Data Query Language), and add SAVEPOINT to TCL. The course follows the four-group version above.
DROP TABLE t(DDL): the table itself is gone, structure and data.TRUNCATE TABLE t(DDL): the table stays but all rows are removed.DELETE FROM t WHERE …(DML): removes the chosen rows only.
SQL is a standard; each DBMS has a dialect
SQL is the standard language all relational DBMSs use. But Oracle, MySQL, PostgreSQL and SQL Server each have their own implementation of it, so a data type or function in one may not exist in another. Since we use MySQL, look up MySQL's flavour when unsure. A handy reference is w3schools: SQL data types for MySQL, SQL Server and MS Access.
Data types (MySQL)
Like every programming language, SQL has data types; you pick one per column. You don't need to memorise them all: you'll pick them up by using them. Nitish's advice: skim the list once during free time, and in interviews you're rarely asked their exact ranges.
| Type | Use it for |
|---|---|
TINYINT, SMALLINT, INT, BIGINT | Whole numbers of increasing size (INT ≈ ±2.1 billion). |
BOOL / BOOLEAN | True/false (stored as TINYINT(1): 0 or 1). |
DECIMAL(p, s) | Exact numbers with a fixed number of decimals: money, ratings like DECIMAL(2,1) → 4.2. |
FLOAT, DOUBLE | Approximate decimals (scientific values). |
| Type | Use it for |
|---|---|
CHAR(n) | Fixed length text, e.g. CHAR(2) state codes. |
VARCHAR(n) | Variable length text up to n characters: names, emails (VARCHAR(255)). |
TEXT | Long text: reviews, descriptions. |
BLOB | Binary Large OBject: raw bytes such as files/images. |
ENUM('a','b',…) | One value from a fixed list, e.g. ENUM('veg','non-veg'). |
| Type | Format / use |
|---|---|
DATE | YYYY-MM-DD: date of birth. |
TIME | hh:mm:ss |
DATETIME | YYYY-MM-DD hh:mm:ss: order time. |
TIMESTAMP | Like DATETIME, often used with DEFAULT CURRENT_TIMESTAMP. |
YEAR | A 4-digit year. |
Operators
SQL has operators just like other languages. You'll learn them by using them in queries, so there's no need to sit and memorise them; skim once.
Arithmetic
+ - * / %
Comparison
= > < >= <= <>
Note: equality is a single =, and "not equal" is <> (MySQL also accepts !=).
Logical
AND OR NOT IN BETWEEN LIKE EXISTS ALL ANY
Try them in a real SQL engine (runs in your browser):
Comments
Writing comments in SQL is simple:
-- a single-line comment (note the space after --)
/* a multi-line
comment */
SELECT * FROM users; -- comments can also follow code
# MySQL also accepts a hash for single-line comments
Conceptual questions
These check whether you understand why, not just what. Several go one step beyond the video.
You ran DELETE FROM orders; inside a transaction and haven't committed. Can you undo it?
DML changes inside an open transaction can be rolled back. (In MySQL, DDL like DROP/TRUNCATE auto-commits, so it can't be.)
Why is DECIMAL preferred over FLOAT for money?
Rounding errors are unacceptable in money.
What does WHERE dept = NULL return?
NULL means "unknown", so = NULL is never true. Always use IS NULL / IS NOT NULL.
A user should be able to read the sales table but not change it. Which command family handles that?
GRANT SELECT ON sales TO user; is DCL.
VARCHAR(255) vs CHAR(255) for names: what's the practical difference?
Use CHAR only for truly fixed-length codes.