SQL Top 100 Interview Questions
SQL Top 100 Interview Questions
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used to
manage and manipulate relational databases. It allows users to query data,
insert new records, update existing ones, and delete unwanted records.
11. What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only matching rows.
LEFT JOIN returns all rows from the left table and matched rows from the
right.
SELECT Name,
CASE
WHEN Marks >= 50 THEN 'Pass'
ELSE 'Fail'
END AS Status
FROM Students;
A View is just a saved SQL query (virtual table) and doesn't store data.
WITH TopEmployees AS (
SELECT * FROM Employees WHERE Salary > 50000
)
SELECT * FROM TopEmployees;
Example:
BEGIN;
UPDATE Accounts SET Balance = Balance - 100 WHERE ID = 1;
UPDATE Accounts SET Balance = Balance + 100 WHERE ID = 2;
COMMIT;
Isolation – No interference
CHAR(10) will always use 10 bytes, VARCHAR(10) uses only what's needed.
DENSE_RANK() – No gaps
Example:
Example:
SELECT Name,
CASE
WHEN Marks >= 90 THEN 'A'
WHEN Marks >= 75 THEN 'B'
ELSE 'C'
END AS Grade
FROM Students;
PARTITION BY – Used with window functions, does not reduce row count.
SELECT Name,
(SELECT MAX(Marks) FROM Students) AS MaxMarks
FROM Students;
two arguments.
63. What are the different ways to handle NULL values in SQL?
Use IS NULL / IS NOT NULL in conditions.
WHERE is used to filter the result after the JOIN has been performed.
SELECT MAX(Salary)
FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);
Non-clustered Index
Unique Index
Full-text Index
Spatial Index
Global Temp Table ( ##temp ): Accessible to all sessions until the last one
closes.
Atomic
Consistent
Isolated
Durable (ACID)
BEGIN TRANSACTION;
-- SQL statements
COMMIT; -- To save
-- or
ROLLBACK; -- To undo
BEGIN TRY
-- SQL code
END TRY
BEGIN CATCH
-- Error handler
END CATCH
3. Fetch rows.
Horizontal – Row-wise
Vertical – Column-wise