SQL Basics and Interview Preparation
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used to manage and manipulate
relational databases. It is used for:
- Creating and modifying database structures (tables, indexes, views, etc.)
- Inserting, updating, deleting, and retrieving data
- Controlling access to the data
It works with databases like MySQL, PostgreSQL, SQL Server, and Oracle.
2. Real-Time SQL Interview Questions
1. What is the difference between WHERE and HAVING?
2. What is a JOIN? Explain different types of joins.
3. What is normalization?
4. Write a query to find the second highest salary:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
5. What is the difference between DELETE, TRUNCATE, and DROP?
6. What is a subquery?
7. What is a primary key and foreign key?
8. What is an index?
9. Explain the difference between UNION and UNION ALL.
10. How to get duplicate records in a table:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING COUNT(*) > 1;
SQL Basics and Interview Preparation
3. SQL Keywords with Explanation
SELECT
Retrieves data from one or more tables. Example: SELECT * FROM users;
FROM
Specifies the table to select from. Example: SELECT name FROM employees;
WHERE
Filters rows based on condition. Example: SELECT * FROM users WHERE age > 25;
GROUP BY
Groups rows sharing a property. Example: SELECT dept, COUNT(*) FROM employees GROUP BY dept;
HAVING
Filters groups (used with GROUP BY). Example: SELECT dept, COUNT(*) FROM employees GROUP BY
dept HAVING COUNT(*) > 5;
ORDER BY
Sorts the result set. Example: SELECT * FROM users ORDER BY age DESC;
INSERT INTO
Adds new data to a table. Example: INSERT INTO users(name, age) VALUES('John', 30);
UPDATE
Modifies existing data. Example: UPDATE users SET age = 35 WHERE name = 'John';
DELETE
Deletes data. Example: DELETE FROM users WHERE age < 18;
JOIN
Combines rows from multiple tables. Example: SELECT * FROM orders JOIN customers ON
orders.customer_id = customers.id;
SQL Basics and Interview Preparation
INNER JOIN
Returns matching rows from both tables.
LEFT JOIN
Returns all rows from left table + matched from right.
RIGHT JOIN
Returns all from right + matched from left.
FULL JOIN
Returns all rows when there is a match in one table.
UNION
Combines result of two queries, removes duplicates.
UNION ALL
Same as UNION, includes duplicates.
DISTINCT
Removes duplicates. Example: SELECT DISTINCT country FROM users;
IN
Matches any in a list. Example: SELECT * FROM users WHERE country IN ('India', 'USA');
BETWEEN
Filters in a range. Example: SELECT * FROM users WHERE age BETWEEN 18 AND 30;
LIKE
Pattern match with % or _. Example: SELECT * FROM users WHERE name LIKE 'A%';
IS NULL
Checks for NULL. Example: SELECT * FROM users WHERE email IS NULL;
AS
Renames a column or table. Example: SELECT name AS username FROM users;
SQL Basics and Interview Preparation
CASE
Conditional logic. Example: SELECT name, CASE WHEN age > 30 THEN 'Old' ELSE 'Young' END AS
category FROM users;