Mastering SQL with 100 Essential Questions and Examples
SQL (Structured Query Language) is the cornerstone of database management and manipulation. This guide explores 100 practical and theoretical SQL questions across various categories to help you enhance your database skills.
1. Basic SQL Query Questions
- Write a query to fetch all records from a table.
SELECT * FROM table_name;
This query retrieves every record from the specified table.
- Write a query to find the second highest salary in a table.
SELECT MAX(salary) AS second_highest_salary
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
- Write a query to fetch employees whose names start with 'A'.
SELECT * FROM employees WHERE name LIKE 'A%';
- Write a query to calculate the total sales grouped by region.
SELECT region, SUM(sales) AS total_sales
FROM sales_data
GROUP BY region;
- Write a query to fetch all records where the column value is NULL.
SELECT * FROM table_name WHERE column_name IS NULL;
- Write a query to remove duplicate rows from a table.
DELETE FROM table_name
WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP BY column_name);
- Write a query to display records in descending order.
SELECT * FROM table_name ORDER BY column_name DESC;
- Write a query to count the number of rows in a table.
SELECT COUNT(*) FROM table_name;
- Write a query to join two tables.
SELECT employees.name, departments.department_name
FROM employees
JOIN departments
ON employees.department_id = departments.id;
-
Write a query to retrieve the first three rows from a table.
SELECT * FROM table_name LIMIT 3;
2. SQL Performance Questions
What is query optimization?
Query optimization involves modifying a query to improve its execution time and efficiency.-
How can you improve the performance of a SQL query?
- Use indexes.
- Avoid
SELECT *
. - Optimize joins.
- Use appropriate data types.
- Analyze the execution plan.
What is the purpose of indexing?
Indexing improves the speed of data retrieval operations on a database table.-
What are the drawbacks of indexing?
- Increased storage requirements.
- Slower data modification operations like
INSERT
andDELETE
.
-
How do you analyze the execution plan of a query?
Use theEXPLAIN
keyword to view the execution plan:
EXPLAIN SELECT * FROM table_name;
What is query caching?
Query caching stores the results of queries for reuse, reducing computation time.What is sharding in databases?
Sharding divides a database into smaller, faster, and more manageable parts called shards.-
Explain the difference between horizontal and vertical scaling.
- Horizontal scaling adds more machines to handle more data.
- Vertical scaling adds resources (CPU, RAM) to a single machine.
How does partitioning help in database performance?
Partitioning divides a large table into smaller, more manageable parts, improving query performance.What is database replication?
Replication involves copying and maintaining database copies across multiple servers for reliability and redundancy.
3. SQL Functions Questions
What are aggregate functions in SQL?
Aggregate functions perform calculations on multiple rows of data:SUM
,AVG
,COUNT
, etc.-
Explain the difference between
COUNT
,SUM
, andAVG
.-
COUNT
: Counts the number of rows. -
SUM
: Adds values in a column. -
AVG
: Calculates the average.
-
-
How does the
ROUND
function work in SQL?
SELECT ROUND(column_name, 2) FROM table_name;
-
What is the
LENGTH
function used for?
It calculates the number of characters in a string:
SELECT LENGTH(column_name) FROM table_name;
-
Explain the use of the
CASE
statement in SQL.
SELECT name, CASE WHEN salary > 5000 THEN 'High' ELSE 'Low' END AS salary_status FROM employees;
-
What is the difference between
COALESCE
andISNULL
?-
COALESCE
: Returns the first non-null value from a list. -
ISNULL
: Checks for null and replaces with a specified value.
-
-
How do you use string functions like
UPPER
andLOWER
?
SELECT UPPER(column_name) AS upper_case, LOWER(column_name) AS lower_case FROM table_name;
-
What is the purpose of the
NOW()
function?
Returns the current date and time:
SELECT NOW();
-
Explain the use of the
CONCAT
function.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
-
What is the difference between
TRUNCATE
andDELETE
?-
TRUNCATE
: Removes all rows from a table without logging individual row deletions. -
DELETE
: Removes rows with a condition and logs each deletion.
-
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
Top comments (0)