DBMS SQL
DBMS SQL
6. Write the query to select all records from a table to retrieve data:
We will use the COUNT() function to count the number of rows. The statement is given
below:
SELECT COUNT(*) FROM tableName;
14. Write the query to group records and calculate aggregate functions:
15. Write the query to limit the number of rows returned in a result set.
We will use the LIMIT clause to limit the number of rows. The statement is given below:
19. Write the query to retrieve the maximum value from a column:
20. Write the query to retrieve rows with values within a specified range:
22. Write the query to search for patterns in a column using wildcard characters:
23. Write the query to filter data based on aggregate functions in a GROUP BY query:
25. Write the query to combine the result sets of two or more SELECT statements:
27. Write the query to delete all rows from a table, but keeps the table structure:
28. Write the query to create an index on one or more columns of a table:
We will use the NULLIF() function to do so, the statement is given below:
27. Delete all rows from a table, but keep the table structure:
- In a testing environment, how would you clear out all test data from the "user" table before
running a new round of tests?
31. Return null if two expressions are equal, otherwise, return the first expression:
- In a voting system, how would you handle cases where two candidates receive the same number
of votes, returning null to indicate a tie?
Answers:
Table – EmployeeSalary
421 P1 12000 0
1. Write an SQL query to fetch the EmpId and FullName of all the employees working under the
Manager with id – ‘986’.
SELECT EmpId, FullName FROM EmployeeDetails WHERE ManagerId = 986;
2. Write an SQL query to fetch the different projects available from the EmployeeSalary table.
SELECT DISTINCT(Project) FROM EmployeeSalary;
3. Write an SQL query to fetch the count of employees working in project ‘P1’.
SELECT COUNT(*) FROM EmployeeSalary WHERE Project = 'P1';
4. Write an SQL query to find the maximum, minimum, and average salary of the employees.
SELECT Max(Salary), Min(Salary), AVG(Salary) FROM EmployeeSalary;
5. Write an SQL query to find the employee id whose salary lies in the range of 9000 and 15000.
SELECT EmpId, Salary FROM EmployeeSalary WHERE Salary BETWEEN 9000 AND 15000;
6. Write an SQL query to fetch those employees who live in Toronto and work under the manager
with ManagerId – 321.
SELECT EmpId, City, ManagerId FROM EmployeeDetails WHERE City='Toronto' AND
ManagerId='321';
7. Write an SQL query to fetch all the employees who either live in California or work under a
manager with ManagerId – 321.
SELECT EmpId, City, ManagerId FROM EmployeeDetails WHERE City='California' OR
ManagerId='321';
8. Write an SQL query to fetch all those employees who work on Projects other than P1.
SELECT EmpId FROM EmployeeSalary WHERE NOT Project='P1';
Or using the ‘not equal to’ operator-
SELECT EmpId FROM EmployeeSalary WHERE Project <> 'P1';
9. Write an SQL query to display the total salary of each employee adding the Salary with Variable
value.
SELECT EmpId, Salary+Variable as TotalSalary FROM EmployeeSalary;
10. Write an SQL query to fetch the employees whose name begins with any two characters,
followed by a text “hn” and ends with any sequence of characters.
SELECT FullName FROM EmployeeDetails WHERE FullName LIKE ‘__hn%’;
11. Write an SQL query to fetch all the EmpIds which are present in either of the tables –
‘EmployeeDetails’ and ‘EmployeeSalary’.
SELECT EmpId FROM EmployeeDetails UNION SELECT EmpId FROM EmployeeSalary;
12. Write an SQL query to fetch common records between two tables.
SELECT * FROM EmployeeSalary INTERSECT SELECT * FROM ManagerSalary;
MySQL – Since MySQL doesn’t have INTERSECT operator so we can use the subquery-
13. Write an SQL query to fetch records that are present in one table but not in another table.
SELECT * FROM EmployeeSalary MINUS SELECT * FROM ManagerSalary;
MySQL – Since MySQL doesn’t have a MINUS operator so we can use LEFT join-
14. Write an SQL query to fetch the EmpIds that are present in both the tables – ‘EmployeeDetails’
and ‘EmployeeSalary.
SELECT EmpId FROM EmployeeDetails where EmpId IN (SELECT EmpId FROM EmployeeSalary);
15. Write an SQL query to fetch the EmpIds that are present in EmployeeDetails but not in
EmployeeSalary.
SELECT EmpId FROM EmployeeDetails where EmpId Not IN (SELECT EmpId FROM
EmployeeSalary);
16. Write an SQL query to fetch the employee’s full names and replace the space with ‘-’.
SELECT REPLACE(FullName, ' ', '-') FROM EmployeeDetails;
17. Write an SQL query to fetch the position of a given character(s) in a field.
SELECT INSTR(FullName, 'Snow') FROM EmployeeDetails;
18. Write an SQL query to display both the EmpId and ManagerId together.
SELECT CONCAT(EmpId, ManagerId) as NewId FROM EmployeeDetails;
19. Write a query to fetch only the first name(string before space) from the FullName column of the
EmployeeDetails table.
SELECT MID(FullName, 1, LOCATE(' ',FullName)) FROM EmployeeDetails;
SQL Server – using SUBSTRING
SELECT SUBSTRING(FullName, 1, CHARINDEX(' ',FullName)) FROM EmployeeDetails;
20. Write an SQL query to uppercase the name of the employee and lowercase the city values.
SELECT UPPER(FullName), LOWER(City) FROM EmployeeDetails;
21. Write an SQL query to find the count of the total occurrences of a particular character – ‘n’ in the
FullName field.
SELECT FullName, LENGTH(FullName) - LENGTH(REPLACE(FullName, 'n', '')) FROM
EmployeeDetails;
22. Write an SQL query to update the employee names by removing leading and trailing spaces.
UPDATE EmployeeDetails SET FullName = LTRIM(RTRIM(FullName));
23. Fetch all the employees who are not working on any project.
SELECT EmpId FROM EmployeeSalary WHERE Project IS NULL;
24. Write an SQL query to fetch employee names having a salary greater than or equal to 5000 and
less than or equal to 10000.
SELECT FullName FROM EmployeeDetails WHERE EmpId IN (SELECT EmpId FROM
EmployeeSalary WHERE Salary BETWEEN 5000 AND 10000);
25. Write an SQL query to find the current date-time.
MySQL-
SELECT NOW();
SQL Server-
SELECT getdate();
Oracle-
SELECT SYSDATE FROM DUAL;
26. Write an SQL query to fetch all the Employee details from the EmployeeDetails table who joined
in the Year 2020.
SELECT * FROM EmployeeDetails WHERE DateOfJoining BETWEEN '2020/01/01' AND
'2020/12/31';
Also, we can extract the year part from the joining date (using YEAR in MySQL)-
SELECT * FROM EmployeeDetails WHERE YEAR(DateOfJoining) = '2020';
27. Write an SQL query to fetch all employee records from the EmployeeDetails table who have a
salary record in the EmployeeSalary table.
SELECT * FROM EmployeeDetails E WHERE EXISTS (SELECT * FROM EmployeeSalary S
WHERE E.EmpId = S.EmpId);
28. Write an SQL query to fetch the project-wise count of employees sorted by project’s count in
descending order.
SELECT Project, count(EmpId) EmpProjectCount FROM EmployeeSalary GROUP BY Project
ORDER BY EmpProjectCount DESC;
29. Write a query to fetch employee names and salary records. Display the employee details even if
the salary record is not present for the employee.
SELECT E.FullName, S.Salary FROM EmployeeDetails E LEFT JOIN EmployeeSalary S ON E.EmpId =
S.EmpId;
30. Write an SQL query to join 3 tables.
SELECT column1, column2 FROM TableA JOIN TableB ON TableA.Column3 = TableB.Column3
JOIN TableC ON TableA.Column4 = TableC.Column4;