What is SQL?
SQL (Structured Query Language) is the standard language for creating, reading, updating and deleting data in relational databases such as MySQL, PostgreSQL, SQL Server and Oracle.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT or SUM in its condition.
What are the types of joins in SQL?
INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN and SELF JOIN. Inner returns matches only; outer joins keep unmatched rows from one or both sides with NULLs.
What is the difference between DELETE, TRUNCATE and DROP?
DELETE removes selected rows and can use WHERE. TRUNCATE removes all rows quickly but keeps the table structure. DROP removes the table itself, including its structure.
What is a primary key?
A primary key uniquely identifies each row in a table. It cannot contain NULL values, and a table can have only one primary key, which may span multiple columns.
What is a foreign key?
A foreign key is a column that references the primary key of another table. It enforces referential integrity, so you cannot insert an order for a customer that does not exist.
What is normalization?
Normalization organises data into related tables to reduce duplication and update anomalies. Freshers should explain 1NF, 2NF and 3NF with a simple example.
What is an index in SQL?
An index is a data structure, usually a B-tree, that lets the database find rows without scanning the whole table. It speeds up reads but adds cost to inserts and updates.
How do you find the second highest salary in SQL?
Use SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees), or DENSE_RANK() over salary in descending order and filter for rank 2.
How do you find duplicate records in a table?
Group by the columns that should be unique and keep groups with more than one row: SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1.
What is a subquery?
A subquery is a query nested inside another query. It can return a single value, a list for IN, or a table used in FROM. A correlated subquery runs once per outer row.
What is the difference between UNION and UNION ALL?
UNION combines results and removes duplicate rows. UNION ALL keeps every row, including duplicates, and is faster because it skips the duplicate check.
What are aggregate functions?
Aggregate functions calculate one value from many rows: COUNT, SUM, AVG, MIN and MAX. They are usually combined with GROUP BY to summarise data per group.
What are window functions?
Window functions such as ROW_NUMBER, RANK, DENSE_RANK and SUM() OVER calculate values across related rows without collapsing them, useful for rankings and running totals.
What is the difference between RANK and DENSE_RANK?
Both give tied rows the same rank. RANK leaves gaps after ties (1, 1, 3), while DENSE_RANK does not (1, 1, 2).
What is a view?
A view is a saved SELECT query that behaves like a virtual table. It simplifies complex queries and can restrict which columns users see, but it usually stores no data itself.
What is the order of execution of a SQL query?
Logically: FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT or TOP. That is why column aliases from SELECT cannot be used in WHERE.
What is a NULL value and how do you check for it?
NULL means a missing or unknown value, not zero or an empty string. Check it with IS NULL or IS NOT NULL, because comparisons like = NULL never return true.
What is a stored procedure?
A stored procedure is a named set of SQL statements saved in the database and run with one call. It can take parameters and is used for repeatable business logic.
How should a fresher prepare for an SQL interview?
Revise joins, grouping, keys and window functions, solve 30-50 practice queries on a sample schema, and be ready to explain one project where you used SQL.