0% found this document useful (0 votes)
17 views5 pages

SQ L Basic Queries

The document provides sample questions and answers related to SQL queries. It contains 26 questions about writing SQL queries to retrieve data from tables based on various conditions. The questions cover concepts like aggregation, joins, sorting, limiting records and more.

Uploaded by

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

SQ L Basic Queries

The document provides sample questions and answers related to SQL queries. It contains 26 questions about writing SQL queries to retrieve data from tables based on various conditions. The questions cover concepts like aggregation, joins, sorting, limiting records and more.

Uploaded by

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

EmployeeInfo Table:

EmpI EmpFnam EmpLnam Departmen Projec Gende


Address DOB
D e e t t r
Hyderabad(HY 01/12/197
1 Sanjay Mehra HR P1 M
D) 6
02/05/196
2 Ananya Mishra Admin P2 Delhi(DEL) F
8
01/01/198
3 Rohan Diwan Account P3 Mumbai(BOM) M
0
Hyderabad(HY 02/05/199
4 Sonia Kulkarni HR P1 F
D) 2
03/07/199
5 Ankit Kapoor Admin P2 Delhi(DEL) M
4
EmployeePosition Table:
EmpID EmpPosition DateOfJoining Salary
1 Manager 01/05/2019 500000
2 Executive 02/05/2019 75000
3 Manager 01/05/2019 90000
2 Lead 02/05/2019 85000
1 Executive 01/05/2019 300000
Let us get started!

MySQL DBA Certification Training


 Instructor-led Sessions
 Real-life Case Studies
 Assignments
 Lifetime Access
Explore Curriculum

Q1. Write a query to fetch the EmpFname from the EmployeeInfo table
in upper case and use the ALIAS name as EmpName.
1 SELECT UPPER(EmpFname) AS EmpName FROM EmployeeInfo;
Q2. Write a query to fetch the number of employees working in the
department ‘HR’.
1 SELECT COUNT(*) FROM EmployeeInfo WHERE Department = 'HR';
Q3. Write a query to get the current date.
You can write a query as follows in SQL Server:
1 SELECT GETDATE();
You can write a query as follows in MySQL:
1 SELECT SYSTDATE();
Q4. Write a query to retrieve the first four characters of EmpLname
from the EmployeeInfo table.
1 SELECT SUBSTRING(EmpLname, 1, 4) FROM EmployeeInfo;
Q5. Write a query to fetch only the place name(string before brackets)
from the Address column of EmployeeInfo table.
Using the MID function in MySQL
1 SELECT MID(Address, 0, LOCATE('(',Address)) FROM EmployeeInfo;
Using SUBSTRING
1 SELECT SUBSTRING(Address, 1, CHARINDEX('(',Address)) FROM EmployeeInfo;
Q6. Write a query to create a new table which consists of data and
structure copied from the other table.
Using the SELECT INTO command:
Databases Training

SQL ESSENTIALS TRAINING & CERTIFICATION


SQL Essentials Training & Certification
Reviews
5(6786)

MYSQL DBA CERTIFICATION TRAINING


MySQL DBA Certification Training
Reviews
5(3730)

MONGODB CERTIFICATION TRAINING


MongoDB Certification Training
Reviews
4(15269)

TERADATA CERTIFICATION TRAINING


Teradata Certification Training
Reviews
5(2413)

APACHE CASSANDRA CERTIFICATION TRAINING


Apache Cassandra Certification Training
Reviews
5(12340)

MASTERING NEO4J GRAPH DATABASE CERTIFICATION TRAINING


Mastering Neo4j Graph Database Certification Training
Reviews
5(799)
Next
1 SELECT * INTO NewTable FROM EmployeeInfo WHERE 1 = 0;
Using the CREATE command in MySQL:
1 CREATE TABLE NewTable AS SELECT * FROM EmployeeInfo;
Q7. Write q query to find all the employees whose salary is between
50000 to 100000.
1 SELECT * FROM EmployeePosition WHERE Salary BETWEEN '50000' AND '100000';
Q8. Write a query to find the names of employees that begin with ‘S’
1 SELECT * FROM EmployeeInfo WHERE EmpFname LIKE 'S%';
Q9. Write a query to fetch top N records.
By using the TOP command in SQL Server:
1 SELECT TOP N * FROM EmployeePosition ORDER BY Salary DESC;
By using the LIMIT command in MySQL:
1 SELECT * FROM EmpPosition ORDER BY Salary DESC LIMIT N;
Q10. Write a query to retrieve the EmpFname and EmpLname in a
single column as “FullName”. The first name and the last name must
be separated with space.
1 SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmployeeInfo;
Q11. Write a query find number of employees whose DOB is between
02/05/1970 to 31/12/1975 and are grouped according to gender
SELECT COUNT(*), Gender FROM EmployeeInfo WHERE DOB BETWEEN '02/05/1970 '
1 AND '31/12/1975' GROUP BY Gender;
Q12. Write a query to fetch all the records from the EmployeeInfo
table ordered by EmpLname in descending order and Department in
the ascending order.
To order the records in ascending and descnding order, you have to use the ORDER
BY statement in SQL.
1 SELECT * FROM EmployeeInfo ORDER BY EmpFname desc, Department asc;
Q13. Write a query to fetch details of employees whose EmpLname
ends with an alphabet ‘A’ and contains five alphabets.
To fetch details mathcing a certain value, you have to use the LIKE operator in SQL.
1 SELECT * FROM EmployeeInfo WHERE EmpLname LIKE '____a';
Q14. Write a query to fetch details of all employees excluding the
employees with first names, “Sanjay” and “Sonia” from the
EmployeeInfo table.
1 SELECT * FROM EmployeeInfo WHERE EmpFname NOT IN ('Sanjay','Sonia');
Q15. Write a query to fetch details of employees with the address as
“DELHI(DEL)”.
1 SELECT * FROM EmployeeInfo WHERE Address LIKE 'DELHI(DEL)%';
Q16. Write a query to fetch all employees who also hold the
managerial position.
1 SELECT E.EmpFname, E.EmpLname, P.EmpPosition
2 FROM EmployeeInfo E INNER JOIN EmployeePosition P ON
3 E.EmpID = P.EmpID AND P.EmpPosition IN ('Manager');
Q17. Write a query to fetch department-wise count of employees
sorted by department’s count in ascending order.
1 SELECT Department, count(EmpID) AS EmpDeptCount
FROM EmployeeInfo GROUP BY Department
2 ORDER BY EmpDeptCount ASC;
3
Q18. Write a query to calculate the even and odd records from a
table.
To retrieve the even records from a table, you have to use the MOD() function as
follows:
SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE
1 MOD(rowno,2)=0;
Similarly, to retrieve the odd records from a table, you can write a query as follows:
SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE
1 MOD(rowno,2)=1;
Q19. Write a SQL query to retrieve employee details from
EmployeeInfo table who have a date of joining in the
EmployeePosition table.
1 SELECT * FROM EmployeeInfo E
2 WHERE EXISTS
3 (SELECT * FROM EmployeePosition P WHERE E.EmpId = P.EmpId);
Q20. Write a query to retrieve two minimum and maximum salaries
from the EmployeePosition table.
To retrieve two minimum salaries, you can write a query as below:
1 SELECT DISTINCT Salary FROM EmployeePosition E1
2 WHERE 2 >= (SELECTCOUNT(DISTINCT Salary)FROM EmployeePosition E2
3 WHERE E1.Salary >= E2.Salary) ORDER BY E1.Salary DESC;
To retrieve two maximum salaries, you can write a query as below:
1 SELECT DISTINCT Salary FROM EmployeePosition E1
2 WHERE 2 >= (SELECTCOUNT(DISTINCT Salary) FROM EmployeePosition E2
3 WHERE E1.Salary <= E2.Salary) ORDER BY E1.Salary DESC;
Q21. Write a query to find the Nth highest salary from the table
without using TOP/limit keyword.
1 SELECT Salary
2 FROM EmployeePosition E1
3 WHERE N-1 = (
4 SELECT COUNT( DISTINCT ( E2.Salary ) )
5 FROM EmployeePosition E2
WHERE E2.Salary > E1.Salary );
6
Q22. Write a query to retrieve duplicate records from a table.
1 SELECT EmpID, EmpFname, Department COUNT(*)
2 FROM EmployeeInfo GROUP BY EmpID, EmpFname, Department
3 HAVING COUNT(*) > 1;
Q23. Write a query to retrieve the list of employees working in the
same department.
1 Select DISTINCT E.EmpID, E.EmpFname, E.Department
2 FROM EmployeeInfo E, Employee E1
3 WHERE E.Department = E1.Department AND E.EmpID != E1.EmpID;
Q24. Write a query to retrieve the last 3 records from the
EmployeeInfo table.
1 SELECT * FROM EmployeeInfo WHERE
EmpID <=3 UNION SELECT * FROM
2
(SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC)
3 AS E1 WHERE E1.EmpID <=3;
4
Q25. Write a query to find the third-highest salary from the
EmpPosition table.
1 SELECT TOP 1 salary
2 FROM(
3 SELECT TOP 3 salary
4 FROM employee_table
ORDER BY salary DESC) AS emp
5
ORDER BY salary ASC;
6
Q26. Write a query to display the first and the last record from the
EmployeeInfo table.
To display the first record from the EmployeeInfo table, you can write a query as
follows:
SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MIN(EmpID) FROM
1 EmployeeInfo);
To display the last record from the EmployeeInfo table, you can write a query as
follows:
SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MAX(EmpID) FROM
1 EmployeeInfo);
Q27. Write a query to add email validation to your database
SELECT Email FROM EmployeeInfo WHERE NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]
1 +@[A-Z0-9.-]+.[A-Z]{2,4}’, ‘i’);
Q28. Write a query to retrieve Departments who have less than 2
employees working in it.
SELECT DEPARTMENT, COUNT(EmpID) as 'EmpNo' FROM EmployeeInfo GROUP BY
1 DEPARTMENT HAVING COUNT(EmpD) < 2;
Q29. Write a query to retrieve EmpPostion along with total salaries
paid for each of them.
SELECT EmpPosition, SUM(Salary) from EmployeePosition GROUP BY
1 EmpPosition;
Q30. Write a query to fetch 50% records from the EmployeeInfo table.
1 SELECT *
2 FROM EmployeeInfo WHERE
3 EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);

You might also like