Hook: SQL is the translator between your app and stored data—interviewers love this question because it reveals whether you understand how software actually reads and writes information.
Question: What is SQL?
Answer: SQL stands for Structured Query Language. It is the standard language used to work with relational databases, where data is stored in tables made of rows and columns. With SQL, you can read data, insert new data, update existing data, delete data, and define or change table structure.
Interview-Ready Answer: I’d say SQL is the language used to communicate with relational databases. It is declarative, which means I describe what result I want, and the database decides how to get it. In practice, I use SQL to query data, change data, and manage schema, and the same core language is supported by systems like PostgreSQL, MySQL, SQL Server, and SQLite, even though each one has its own dialect.
Detailed Explanation:
SQL is short for Structured Query Language. It is a declarative language, which means you describe the result you want instead of writing the exact steps to get it. A relational database stores data in tables, and tables are connected through keys, such as a customer id pointing to an order row.
SELECT name FROM customers WHERE id = 7.SELECT finds rows that match a condition.INSERT, UPDATE, and DELETE modify rows.CREATE, ALTER, and DROP change tables and other objects.These statement groups are often described as DDL (Data Definition Language) for structure and DML (Data Manipulation Language) for data. Some teams also mention DCL for permissions and TCL for transaction control.
SQL is not the same thing as a database. It is the language; PostgreSQL, MySQL, SQL Server, and SQLite are database systems that implement it. A common interview point is that SQL databases tend to be best when your data is structured and relationships matter.
| Aspect | SQL | NoSQL |
|---|---|---|
| Data model | Tables | Docs / keys |
| Schema | Usually fixed | Often flexible |
| Strength | Joins, consistency | Scale, flexibility |
| Best for | Orders, finance | Feeds, events |
Use SQL when you need reliable structure, joins between related tables, transactions, and reporting. Use a NoSQL database when the shape of the data changes often or when access patterns are very simple and high-scale.
SQL itself is not one algorithm, so there is no single big-O number for every query. A query on an indexed column is often close to O(log n) because a B-tree index, a balanced tree structure, narrows the search quickly; a full table scan is O(n); and a bad join can approach O(n^2) if the plan must compare many rows. In real systems, the difference is huge: an indexed lookup may finish in a few milliseconds, while a scan of 10 million wide rows can take seconds.
Indexes speed reads but cost space and write time. Adding a few useful indexes can make hot queries much faster, but every extra index also adds maintenance work to INSERT, UPDATE, and DELETE.
NULL means missing or unknown, so = NULL does not work; you must use IS NULL.ORDER BY is required if you need a stable row order; without it, the database may return rows in any order.Real-World Example:
Imagine an e-commerce checkout service on a flash sale. The service uses SQL to read inventory, create the order, and mark stock as reserved. A developer forgets to put the stock check and the update inside one transaction, so two buyers can read the same last item before either update is committed. In production, the symptom is overselling: the database ends up with negative stock, support tickets spike, and logs show duplicate order attempts plus slow query warnings if the inventory lookup is missing an index. Users see a spinner, then a confusing item unavailable message after payment already started, which is the kind of bug that ruins trust fast.
-- Clean start so the script can be re-run in a scratch database.
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS customers;
-- Customers are the parent table: one customer can have many orders.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
-- Orders point back to customers. The foreign key expresses the relationship in SQL.
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
order_total DECIMAL(10,2) NOT NULL,
shipped_at DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
INSERT INTO customers (customer_id, name) VALUES
(1, 'Ava'),
(2, 'Ben'),
(3, 'Cara');
INSERT INTO orders (order_id, customer_id, order_total, shipped_at) VALUES
(101, 1, 49.99, '2026-07-01'),
(102, 1, 19.50, NULL),
(103, 2, 120.00, '2026-07-02');
-- A LEFT JOIN keeps customers even when they have no orders.
-- That is useful for reporting because missing related rows show up as NULLs.
SELECT c.customer_id, c.name, o.order_id, o.order_total, o.shipped_at
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
ORDER BY c.customer_id, o.order_id;
-- Common failure path: comparing to NULL with '=' never matches anything.
-- This is syntactically valid, but logically wrong, so it returns 0 instead of 1.
SELECT COUNT(*) AS bad_null_check
FROM orders
WHERE shipped_at = NULL;
-- Correct way: IS NULL matches missing values.
SELECT COUNT(*) AS correct_null_check
FROM orders
WHERE shipped_at IS NULL;
Follow-up & Tricky Questions:
CREATE and ALTER; DML changes data with commands like INSERT, UPDATE, and DELETE.NULL = NULL return true? No. NULL means unknown or missing, so equality does not apply; use IS NULL instead.ORDER BY; otherwise the database can return rows in any sequence.SELECT * always fine? Not really. It can pull extra columns you do not need and can break application code if the schema changes.Common Mistakes:
= NULL. Correction: use IS NULL or IS NOT NULL.ORDER BY. Correction: result order is not guaranteed unless you ask for it explicitly.Memory Hook: Think of SQL like a restaurant menu: you point to the dish you want, and the kitchen decides how to cook it. You say what data you want, not how to find every row.
Cheat Sheet:
NULL, ORDER BY, and dialect differences are classic interview traps.Practice Tasks:
students table and insert three rows.SELECT that filters by one column and sorts by another.LEFT JOIN to find missing matches with IS NULL.