Interview Q&A · SQL · 10 questions in this topic
SQL Fundamentals — SQL Interview Q&A
The vocabulary every SQL interview opens with — what SQL and databases are, and how the language is organized. Part of SQL Fundamentals · 192 questions in the SQL bank.
What is SQL?
Answer
SQL (Structured Query Language) is the declarative standard language for storing, querying and managing data in relational databases. You describe what result you want (SELECT ... WHERE ... GROUP BY), and the database engine decides how to execute it via its query planner.
It is standardized (SQL:2023 is current) with portable core syntax plus vendor dialects — PostgreSQL, MySQL, SQL Server, SQLite and Oracle all extend the standard differently.
Example
SELECT department, AVG(salary) AS avg_pay FROM employees WHERE age > 30 GROUP BY department HAVING AVG(salary) > 60000;
💡 Say declarative first — describing what, not how — that single word frames the whole language.
Why use a database instead of a spreadsheet or flat files?
Answer
Databases enforce structure (types, constraints), handle concurrent writers safely (transactions, locking), answer ad-hoc questions over millions of rows in milliseconds (indexes, query planner), and guarantee durability (write-ahead logging, crash recovery). Spreadsheets and flat files offer none of these past trivial scale.
The interview shorthand: integrity, concurrency, query power and durability — pick any two to elaborate.
Key points
- 1Integrity — types and constraints reject bad data.
- 2Concurrency — many writers, consistent results.
- 3Scale — indexes and planners over huge data.
- 4Durability — committed data survives crashes.
What is a relational database? What are tables, rows and columns?
Answer
A relational database organizes data into tables (relations) of rows (tuples/records) and columns (attributes), with each column having a type and each row identified by a key. Tables relate through shared key values (foreign keys), and SQL queries combine, filter and aggregate across them.
Codd's relational model (1970) is the theory; the table picture is the practice.
Example
-- employees(id, name, department, salary) -- departments(dept_id, location, head) SELECT e.name, d.location FROM employees e JOIN departments d ON e.department = d.dept_id;
What are the sublanguages of SQL (DDL, DML, DCL, TCL)?
Answer
DDL (Data Definition Language) defines structure: CREATE, ALTER, DROP. DML (Data Manipulation Language) works with rows: SELECT, INSERT, UPDATE, DELETE. DCL (Data Control Language) manages permissions: GRANT, REVOKE. TCL (Transaction Control Language) manages transactions: COMMIT, ROLLBACK, SAVEPOINT.
Interviewers often ask for one example of each — have the four verbs ready.
Key points
- 1DDL — CREATE TABLE, ALTER TABLE, DROP TABLE.
- 2DML — SELECT, INSERT, UPDATE, DELETE.
- 3DCL — GRANT, REVOKE.
- 4TCL — COMMIT, ROLLBACK, SAVEPOINT.
Example
CREATE TABLE t (id INT PRIMARY KEY); -- DDL INSERT INTO t VALUES (1); -- DML COMMIT; -- TCL
💡 One verb per letter is the complete answer; the example seals it.
What is the difference between SQL and MySQL / PostgreSQL / SQLite?
Answer
SQL is the language standard; MySQL, PostgreSQL, SQLite (and SQL Server, Oracle) are database systems (RDBMS) that implement it with their own dialects, storage engines and tooling. PostgreSQL is the feature-rich open-source default (advanced types, window functions, CTEs); MySQL is the ubiquitous web default; SQLite is a serverless single-file engine for embedded/mobile/tests.
Core SELECT/WHERE/GROUP BY/JOIN transfers everywhere; functions, quoting and upsert syntax vary.
Example
-- upsert differs per engine: -- PostgreSQL/SQLite: INSERT ... ON CONFLICT DO UPDATE -- MySQL: INSERT ... ON DUPLICATE KEY UPDATE
💡 Language vs implementation is the distinction; one dialect example proves you have felt it.
How does a SQL query get executed (parse, plan, execute)?
Answer
The engine parses SQL into a tree, validates names/types, then the planner picks the cheapest execution plan (index vs scan, join order and algorithm, aggregation strategy) using statistics — EXPLAIN shows this choice. The executor runs the plan, pulling rows through operators.
This pipeline is why the same logical query can be fast or slow: the plan depends on indexes and statistics, not just your SQL text.
Key points
- 1Parse + validate — syntax and object checks.
- 2Plan — cost-based choice of access paths and join order.
- 3Execute — operators stream rows to the client.
Example
EXPLAIN SELECT * FROM employees WHERE department = 'Sales'; -- Seq Scan vs Index Scan lives in this output
💡 Mention EXPLAIN unprompted — it turns theory into a tool answer.
What is the difference between a database, a schema and a table?
Answer
A database (instance/cluster) is the whole managed system holding many schemas. A schema is a namespace grouping related tables (plus views, functions) — sales.orders vs hr.employees. A table is one structured dataset inside a schema.
In MySQL, DATABASE and SCHEMA are near-synonyms; in PostgreSQL a database contains multiple schemas (public by default).
Example
CREATE SCHEMA sales;
CREATE TABLE sales.orders (id INT PRIMARY KEY, total NUMERIC);What is normalization (1NF, 2NF, 3NF) in one minute?
Answer
Normalization removes redundancy via rules: 1NF — atomic values, no repeating groups. 2NF — 1NF plus every non-key column depends on the whole key (no partial dependency). 3NF — 2NF plus no transitive dependencies (non-key columns depend only on the key). Most production schemas sit at 3NF; analytics often denormalizes deliberately for read speed.
Over-normalization costs joins; under-normalization costs anomalies — 3NF is the default balance.
Key points
- 11NF — atomic cells, one value per cell.
- 22NF — no partial-key dependency.
- 33NF — no transitive dependency.
Example
-- 3NF: orders(customer_id) -> customers(customer_id, city) -- city lives once in customers, not per order
💡 One rule per normal form plus the denormalization caveat is the complete minute.
What is the difference between OLTP and OLAP?
Answer
OLTP (transactional) handles many small concurrent reads/writes on normalized tables — order systems, banking. OLAP (analytical) runs heavy aggregations over huge historical data — dashboards, data warehouses, star schemas.
Design follows the workload: row stores + indexes for OLTP; columnar storage + denormalized marts for OLAP.
Key points
- 1OLTP — many small transactions, normalized, row-oriented.
- 2OLAP — few huge scans, denormalized, columnar.
What is a data type in SQL and how do you choose one?
Answer
Types fix what a column holds and how it compares, sorts and stores: INT/BIGINT, NUMERIC(p,s) for exact money math, VARCHAR(n)/TEXT, DATE/TIMESTAMP (with time-zone care), BOOLEAN, JSON/JSONB, UUID, arrays (PostgreSQL). Choose the narrowest correct type: money is NUMERIC never FLOAT, identifiers are BIGINT/UUID, timestamps carry zones.
Wrong types corrupt silently — floats rounding money, VARCHAR dates mis-sorting — so type choice is a correctness decision, not style.
Example
amount NUMERIC(12,2) NOT NULL, -- money: exact created_at TIMESTAMPTZ NOT NULL, -- moment in time, zoned payload JSONB -- semi-structured, indexable
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission