SQL (Structured Query Language) is the standard language for creating, querying, and managing relational databases. A DBMS (Database Management System) is any software that manages databases — including file-based systems. An RDBMS (Relational DBMS) specifically implements the relational model with tables, keys, and SQL. SQL is declarative: you state WHAT data you want, not HOW to get it. The database engine optimises execution automatically. SQL is case-insensitive for keywords but case-sensitive for data values in most systems.
DBMS vs RDBMS vs NoSQL
| Type | Data model | Query language | Examples |
|---|---|---|---|
| DBMS | Any (file, hierarchical, network) | Varies | IMS, ADABAS, early file systems |
| RDBMS | Tables with rows and columns — relational model | SQL | PostgreSQL, MySQL, Oracle, SQL Server, SQLite |
| NoSQL | Documents, key-value, graph, wide-column | Custom (MongoDB Query, CQL, Gremlin) | MongoDB, Redis, Cassandra, Neo4j, DynamoDB |
SQL vs NoSQL — when to use which
Use SQL/RDBMS when: data is structured and relational, strong consistency is required, complex joins and transactions are needed, schema is stable. Use NoSQL when: data is unstructured or semi-structured (JSON, logs), horizontal scaling is critical, schema changes frequently, low-latency key-value lookup is the primary operation.
SQL statement categories
| Category | Full name | Commands | Purpose |
|---|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE, RENAME | Define and modify database structure (schema) |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE | Add, change, remove data rows |
| DQL | Data Query Language | SELECT | Retrieve data (often grouped under DML) |
| DCL | Data Control Language | GRANT, REVOKE | Manage user permissions and access rights |
| TCL | Transaction Control Language | COMMIT, ROLLBACK, SAVEPOINT | Manage transaction boundaries |
SQL syntax rules and conventions
SQL syntax: keywords, identifiers, comments, formatting
-- Single-line comment
/* Multi-line
comment */
-- SQL is case-INSENSITIVE for keywords:
-- SELECT = select = Select = sElEcT (all equivalent)
-- But data values ARE case-sensitive:
-- WHERE Name = 'Ravi' ≠ WHERE Name = 'ravi'
-- Basic statement structure:
SELECT column1, column2 -- Columns to retrieve
FROM table_name -- Source table
WHERE condition -- Filter rows (optional)
ORDER BY column1 ASC; -- Sort result (optional)
-- Every SQL statement ends with semicolon (;)
-- Identifier quoting (for reserved words or special chars):
SELECT "select", "my-column", "Table Name" -- PostgreSQL/standard
FROM [my table] -- SQL Server
FROM `my table`; -- MySQL
-- String literals use single quotes (ANSI standard):
WHERE Name = 'Ravi Kumar' -- Correct
WHERE Name = "Ravi Kumar" -- Non-standard (works in MySQL, not Oracle/PostgreSQL)
-- NULL is not a value — use IS NULL, not = NULL:
WHERE DeptID IS NULL -- Correct
WHERE DeptID = NULL -- ALWAYS FALSE (NULL != NULL in SQL)
-- Aliases
SELECT Name AS FullName, Salary * 12 AS AnnualSalary FROM Employee;
SELECT Name FullName FROM Employee; -- AS is optionalThe SELECT statement — anatomy and execution order
Full SELECT anatomy with all optional clauses
-- Written order: Logical execution order:
-- 1. SELECT 5. SELECT (choose columns, compute expressions)
-- 2. FROM 1. FROM (identify source tables)
-- 3. JOIN 1. JOIN (combine tables)
-- 4. WHERE 2. WHERE (filter rows)
-- 5. GROUP BY 3. GROUP BY (form groups)
-- 6. HAVING 4. HAVING (filter groups)
-- 7. ORDER BY 6. ORDER BY (sort)
-- 8. LIMIT 7. LIMIT/OFFSET (paginate)
-- Full example showing all clauses:
SELECT DISTINCT -- Remove duplicate output rows
d.DeptName,
COUNT(e.EmpID) AS HeadCount,
AVG(e.Salary) AS AvgSalary,
MAX(e.Salary) AS TopSalary
FROM Employee e
INNER JOIN Department d ON e.DeptID = d.DeptID
WHERE e.Status = 'Active'
AND e.JoinDate > '2020-01-01'
GROUP BY d.DeptName
HAVING COUNT(e.EmpID) > 5
ORDER BY AvgSalary DESC
LIMIT 10 OFFSET 0; -- First page of 10 results
-- DISTINCT vs GROUP BY:
SELECT DISTINCT DeptID FROM Employee; -- Unique dept values
SELECT DeptID, COUNT(*) FROM Employee GROUP BY DeptID; -- Unique + countsWhy execution order matters
Because SELECT is executed in step 5, you CANNOT reference a SELECT alias in WHERE (step 2 runs before step 5). Example: SELECT Salary * 12 AS Annual ... WHERE Annual > 100000 FAILS. Use WHERE Salary * 12 > 100000 instead. However, you CAN use SELECT aliases in ORDER BY (runs after SELECT).
Practice questions
- Can you use a SELECT alias in the WHERE clause? (Answer: No — WHERE executes before SELECT in the logical order. Use the full expression in WHERE. You can use aliases in ORDER BY and HAVING (some DBMS allow it in HAVING).)
- What is the difference between DBMS and RDBMS? (Answer: DBMS is any database management software. RDBMS specifically implements the relational model — tables, rows, columns, SQL, and referential integrity. All RDBMS are DBMS but not all DBMS are RDBMS.)
- Why does WHERE Name = NULL never return any rows? (Answer: In SQL, NULL represents unknown. NULL = NULL evaluates to UNKNOWN (not TRUE). Use IS NULL or IS NOT NULL — these are the only operators that handle NULL correctly.)
- TRUNCATE vs DROP vs DELETE — key differences: (Answer: DROP removes table structure + data. TRUNCATE removes all rows, keeps structure, minimal logging, fast, no WHERE clause. DELETE removes specific rows, fully logged, supports WHERE, can be rolled back.)
- SQL is declarative. What does this mean? (Answer: You specify WHAT result you want, not HOW to compute it. The database query optimiser decides the execution plan. Compare to procedural code where you write step-by-step instructions.)
On LumiChats
LumiChats can explain any SQL concept, write queries for any scenario, and debug SQL errors. Try: 'Why is my WHERE clause not working with a NULL check?' or 'Explain the difference between TRUNCATE and DELETE with examples.'
Try it free