0% found this document useful (0 votes)
20 views3 pages

25 SQL Questions As Per JD

The document provides a comprehensive overview of SQL concepts, including definitions, differences between SQL and MySQL, and various SQL commands and queries. It covers beginner to advanced-level topics such as JOIN clauses, subqueries, primary keys, and performance optimization techniques. Additionally, it includes practical SQL query examples for common tasks like retrieving unique values, updating records, and finding duplicates.

Uploaded by

vikhepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

25 SQL Questions As Per JD

The document provides a comprehensive overview of SQL concepts, including definitions, differences between SQL and MySQL, and various SQL commands and queries. It covers beginner to advanced-level topics such as JOIN clauses, subqueries, primary keys, and performance optimization techniques. Additionally, it includes practical SQL query examples for common tasks like retrieving unique values, updating records, and finding duplicates.

Uploaded by

vikhepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Beginner-Level (Easy)

1. What is SQL, and how does it differ from MySQL?


* SQL (Structured Query Language) is a standard language for managing and
manipulating databases, while MySQL is a specific database management system (DBMS)
that uses SQL as its query language.
2. Explain the difference between WHERE and HAVING clauses in SQL.
* WHERE is used to filter rows before any grouping is applied. HAVING is used to
filter groups created by GROUP BY after aggregation.
Write an SQL query to select all records from a table named Employees.
sql
Copy code
SELECT * FROM Employees;
3. How would you retrieve unique values from a column in a table?
sql
Copy code
SELECT DISTINCT column_name FROM table_name;
4. 5. What is the purpose of the JOIN clause in SQL, and name its different types.
* The JOIN clause combines rows from two or more tables based on a related
column. Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
Write a query to fetch only the first 10 rows from a table named Orders.
sql
Copy code
SELECT * FROM Orders LIMIT 10;
6. How would you update a specific record in a table named Customers?
sql
Copy code
UPDATE Customers SET column_name = new_value WHERE condition;
7. 8. What is an INDEX, and how does it help in SQL queries?
* An INDEX speeds up the retrieval of rows by using pointers. It enhances the
performance of queries but may slow down INSERT and UPDATE operations.
Write an SQL statement to create a new table named Products with columns ProductID,
ProductName, and Price.
sql
Copy code
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2)
);
9. 10. Explain the difference between DELETE, DROP, and TRUNCATE commands.
* DELETE removes rows based on a condition and can be rolled back.
* TRUNCATE removes all rows but keeps the table structure and is faster than
DELETE.
* DROP deletes the table structure and all data permanently.
________________

Intermediate-Level (Medium)
Write an SQL query to find employees who have joined in the last 30 days in a table
named Employees with a JoinDate column.
sql
Copy code
SELECT * FROM Employees WHERE JoinDate >= NOW() - INTERVAL 30 DAY;
11. 12. What is a subquery, and how is it used in SQL? Provide an example.
A subquery is a query within another query, often used to filter results. Example:
sql
Copy code
SELECT * FROM Employees WHERE DepartmentID IN (SELECT DepartmentID FROM Departments
WHERE Location = 'Pune');
* How would you retrieve data from multiple tables with matching records? Write
a query using an INNER JOIN.
sql
Copy code
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
13. Write an SQL query to calculate the average salary in the Employees table and
group the result by Department.
sql
Copy code
SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department;
14. 15. Explain UNION vs UNION ALL. How do they differ?
* UNION combines the results of two queries and removes duplicates. UNION ALL
includes all records, including duplicates.
16. What is a primary key constraint, and why is it important in SQL?
* A primary key uniquely identifies each row in a table, ensuring that no
duplicate values are in the key column(s), which is essential for data integrity.
How would you retrieve the 5th highest salary from a table named Salaries?
sql
Copy code
SELECT DISTINCT Salary
FROM Salaries
ORDER BY Salary DESC
LIMIT 1 OFFSET 4;
17. Write a query to find duplicate records in a table named Orders with columns
OrderID, CustomerID, and OrderDate.
sql
Copy code
SELECT CustomerID, OrderDate, COUNT(*)
FROM Orders
GROUP BY CustomerID, OrderDate
HAVING COUNT(*) > 1;
18. 19. What is a stored procedure, and why would you use it?
* A stored procedure is a precompiled set of SQL statements saved in the
database. It is used to encapsulate complex logic, improve performance, and reduce
repetitive code.
Write an SQL query to fetch records where the OrderDate is within the last 7 days.
sql
Copy code
SELECT * FROM Orders WHERE OrderDate >= NOW() - INTERVAL 7 DAY;
20. ________________

Advanced-Level (Very Hard)


21. Explain the difference between EXISTS and IN clauses and write an example query
for each.
* EXISTS is used to check if a subquery returns any rows; it’s often faster in
correlated subqueries.
* IN is used to match a column against a list of values.
sql
Copy code
-- USING EXISTS
SELECT * FROM Employees E
WHERE EXISTS (SELECT 1 FROM Departments D WHERE D.DepartmentID = E.DepartmentID);
-- USING IN
SELECT * FROM Employees
WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Location =
'Pune');
22. 23. How would you optimize an SQL query for performance? List at least three
methods.
* Creating indexes on frequently queried columns.
* Avoiding unnecessary columns in SELECT statements.
* Using EXISTS instead of IN when working with subqueries on large datasets.
Write an SQL query using a LEFT JOIN to find all customers who have not placed any
orders in the Customers and Orders tables.
sql
Copy code
SELECT Customers.CustomerID, Customers.CustomerName
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderID IS NULL;
24. 25. What is a CROSS JOIN, and how does it differ from other joins? Provide an
example query.
A CROSS JOIN returns the Cartesian product of both tables, resulting in all
possible combinations of rows.
sql
Copy code
SELECT * FROM Employees CROSS JOIN Departments;
* Write an SQL query that finds the second most frequent product sold in a table
Sales with columns ProductID and Quantity.
sql
Copy code
SELECT ProductID
FROM Sales
GROUP BY ProductID
ORDER BY COUNT(*) DESC
LIMIT 1 OFFSET 1;
26. These answers cover various SQL concepts relevant to support roles where SQL is
essential for troubleshooting, reporting, and data validation.
4o

You might also like