Basic Interview Questions

What is the difference between DBMS and RDBMS?

DBMSRDBMS
stores data as files, documents or hierarchiesstores data in related tables (rows and columns)
relationships not enforcedrelationships enforced with keys and constraints
normalization not applicablesupports normalization
usually single user, small datamulti-user, large data, ACID transactions
e.g. file systems, early XML storese.g. MySQL, PostgreSQL, SQL Server, Oracle

What is SQL, and what are its command categories?

SQL (Structured Query Language) is the standard language to define, query and manipulate relational data. Its commands are grouped into:

  • DDL: CREATE, ALTER, DROP, TRUNCATE
  • DQL: SELECT
  • DML: INSERT, UPDATE, DELETE
  • DCL: GRANT, REVOKE
  • TCL: COMMIT, ROLLBACK, SAVEPOINT

What is a primary key? How is it different from a unique key?

Primary keyUnique key
NULL allowed?noyes (one or many, depending on the database)
How many per table?onemany
Clustered by default?often (SQL Server, InnoDB)no
Purposemain row identifier, target of foreign keysprevent duplicates in alternate keys (e.g. email)

What is a foreign key?

A column (or set of columns) in a child table that references the primary key of a parent table. It enforces referential integrity: you cannot insert an order for a customer that does not exist, or delete a customer who still has orders (unless ON DELETE CASCADE / SET NULL is defined).

What is NULL? Is it the same as 0 or an empty string?

No. NULL means a value is unknown or missing. 0 and '' are actual values. Any comparison with NULL (even NULL = NULL) is unknown, so you must test with IS NULL / IS NOT NULL. Aggregates except COUNT(*) ignore NULLs.

Example

Compare NULL with =, IS NULL and COALESCE:

SELECT NULL = NULL AS EqualsTest, NULL IS NULL AS IsNullTest, COALESCE(NULL, 'fallback') AS Coalesced;

Result: 1 row

EqualsTestIsNullTestCoalesced
NULL1fallback

What is the difference between DELETE, TRUNCATE and DROP?

DELETETRUNCATEDROP
CategoryDMLDDLDDL
WHERE allowedyesnono
Removesselected rowsall rowstable + data + indexes
Rollbackyesdatabase-dependentdatabase-dependent
Triggers fireyesnono
Speedslow (logged per row)fastfast

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregates.

Example

WHERE removes rows first, HAVING then removes whole groups:

SELECT Department, AVG(Salary) AS AvgSal
FROM Employees
WHERE HireDate < '2022-01-01'     -- rows
GROUP BY Department
HAVING AVG(Salary) > 60000;       -- groups

Result: 3 rows

DepartmentAvgSal
Finance80000
Sales63666.6667
Support67000

What is the difference between UNION and UNION ALL?

Both stack the results of two queries. UNION removes duplicate rows (extra sort/hash step); UNION ALL keeps all rows and is faster. Use UNION ALL unless you need de-duplication.

What is the difference between CHAR and VARCHAR?

CHAR(n) is fixed length: values are padded with spaces to n characters. Good for fixed codes (country code, gender). VARCHAR(n) stores only the actual characters plus a small length prefix. Good for names, emails and other variable text.

In what order is a SELECT query executed?

FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. This is why a column alias from SELECT cannot be used in WHERE but can be used in ORDER BY.

What are constraints? Name the common ones.

Rules enforced by the database on column values: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK and DEFAULT. They guarantee data integrity regardless of which application writes the data.

What is a composite key?

A primary key made of two or more columns, used when no single column is unique. Example: PRIMARY KEY (OrderID, ProductID) in an order-line table.

What are candidate, super, alternate and surrogate keys?

  • Super key: any column set that uniquely identifies a row.
  • Candidate key: a minimal super key.
  • Alternate key: a candidate key not chosen as the primary key.
  • Surrogate key: an artificial identifier (auto-increment, UUID) with no business meaning.

What is AUTO_INCREMENT / IDENTITY?

A column property that generates the next number automatically on insert: AUTO_INCREMENT (MySQL), IDENTITY(1,1) (SQL Server), SERIAL/GENERATED AS IDENTITY (PostgreSQL), AUTOINCREMENT (SQLite). Values can have gaps after rollbacks or deletes.