EmployeeInfo Table:
create table EmployeeInfo (EmpId int, EmpFname varchar(20), EmpLname
varchar(20), Department varchar(10), Project varchar(10), Address
varchar(15), DOB date, Gender char(5));
insert into EmployeeInfo (EmpID, EmpFname, EmpLname, Department,
Project, Address, DOB, Gender)
values (1, 'Sanjay', 'Mehra', 'HR', 'P1', 'Hyderabad(HYD)', '1976-12-01',
'M'),
(2, 'Ananya', 'Mishra', 'Admin', 'P2', 'Delhi(DEL)', '1968-05-02', 'F'),
(3, 'Rohan', 'Diwan', 'Account', 'P3', 'Mumbai(BOM)', '1980-01-01',
'M'),
(4, 'Sonia', 'Kulkarni', 'HR', 'P1', 'Hyderabad(HYD)', '1992-02-05', 'F'),
(5, 'Ankite', 'Kapoor', 'Admin', 'P2', 'Delhi(DEL)', '1994-07-03', 'M');
EmpI EmpFna EmpLna Departme Proje Gende
Address DOB
D me me nt ct 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/2022 500000
2 Executive 02/05/2022 75000
3 Manager 01/05/2022 90000
2 Lead 02/05/2022 85000
1 Executive 01/05/2022 300000
Let us start by taking a look at some of the most frequently asked SQL Query
interview questions,
Write a query to fetch the EmpFname from the EmployeeInfo table in the
upper case and use the ALIAS name as EmpName.
Write a query to fetch the number of employees working in the department
‘HR’.
Write a query to get the current date.
Write a query to retrieve the first four characters of EmpLname from the
EmployeeInfo table.
Write a query to fetch only the place name(string before brackets) from the
Address column of EmployeeInfo table.
Write a query to create a new table that consists of data and structure copied
from the other table.
Write q query to find all the employees whose salary is between 50000 to
100000.
Write a query to find the names of employees that begin with ‘S’
Write a query to fetch top N records.
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.
Q1. Write a query to fetch the EmpFname from the EmployeeInfo table
in upper case and use the ALIAS name as EmpName.
1SELECTUPPER(EmpFname) ASEmpName FROMEmployeeInfo;
Q2. Write a query to fetch the number of employees working in the
department ‘HR’.
1SELECTCOUNT(*) FROMEmployeeInfo WHEREDepartment = 'HR';
Q3. Write a query to get the current date.
You can write a query as follows in SQL Server:
1SELECTGETDATE();
You can write a query as follows in MySQL:
1SELECTSYSTDATE();
Q4. Write a query to retrieve the first four characters of EmpLname
from the EmployeeInfo table.
1SELECTSUBSTRING(EmpLname, 1, 4) FROMEmployeeInfo;
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
1SELECTMID(Address, 0, LOCATE('(',Address)) FROMEmployeeInfo;
Using SUBSTRING
1SELECTSUBSTRING(Address, 1, CHARINDEX('(',Address)) FROMEmployeeInfo;
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:
1SELECT* INTONewTable FROMEmployeeInfo WHERE1 = 0;
Using the CREATE command in MySQL:
1CREATETABLENewTable ASSELECT* FROMEmployeeInfo;
Q7. Write q query to find all the employees whose salary is between
50000 to 100000.
1SELECT* FROMEmployeePosition WHERESalary BETWEEN'50000'AND'100000';
Q8. Write a query to find the names of employees that begin with ‘S’
1SELECT* FROMEmployeeInfo WHEREEmpFname LIKE'S%';
Q9. Write a query to fetch top N records.
By using the TOP command in SQL Server:
1SELECTTOPN * FROMEmployeePosition ORDERBYSalary DESC;
By using the LIMIT command in MySQL:
1SELECT* FROMEmpPosition ORDERBYSalary DESCLIMIT 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.
1SELECTCONCAT(EmpFname, ' ', EmpLname) AS'FullName'FROMEmployeeInfo;
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
1SELECTCOUNT(*), Gender FROMEmployeeInfo WHEREDOB BETWEEN'02/05/1970 'AND'31/12/1975'GR
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.
1SELECT* FROMEmployeeInfo ORDERBYEmpFname 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.
1SELECT* FROMEmployeeInfo WHEREEmpLname 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.
1SELECT* FROMEmployeeInfo WHEREEmpFname NOTIN('Sanjay','Sonia');
Want to upskill yourself to get ahead in your career? Check out this video
Top 10 Technologies to Learn in 2022 |
Edureka
Q15. Write a query to fetch details of employees with the
address as “DELHI(DEL)”.
1SELECT* FROMEmployeeInfo WHEREAddress LIKE'DELHI(DEL)%';
Q16. Write a query to fetch all employees who also hold
the managerial position.
[Link], [Link], [Link]
2FROMEmployeeInfo E INNERJOINEmployeePosition P ON
[Link] = [Link] [Link] IN('Manager');
Q17. Write a query to fetch the department-wise count of
employees sorted by department’s count in ascending
order.
1SELECTDepartment, count(EmpID) ASEmpDeptCount
2FROMEmployeeInfo GROUPBYDepartment
3ORDERBYEmpDeptCount ASC;
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:
1SELECTEmpID FROM(SELECTrowno, EmpID fromEmployeeInfo) WHEREMOD(rowno,2)=0;
Similarly, to retrieve the odd records from a table, you can write a query as follows:
1SELECTEmpID FROM(SELECTrowno, EmpID fromEmployeeInfo) WHEREMOD(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.
1SELECT* FROMEmployeeInfo E
2WHEREEXISTS
3(SELECT* FROMEmployeePosition P [Link] = [Link]);
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:
1SELECTDISTINCTSalary FROMEmployeePosition E1
2 WHERE2 >= (SELECTCOUNT(DISTINCTSalary)FROMEmployeePosition E2
3 [Link] >= [Link]) [Link] DESC;
To retrieve two maximum salaries, you can write a query as
below:
1SELECTDISTINCTSalary FROMEmployeePosition E1
2 WHERE2 >= (SELECTCOUNT(DISTINCTSalary) FROMEmployeePosition E2
3 [Link] <= [Link]) [Link] DESC;
Q21. Write a query to find the Nth highest salary from the
table without using TOP/limit keyword.
1SELECTSalary
2FROMEmployeePosition E1
3WHEREN-1 = (
4 SELECTCOUNT( DISTINCT( [Link] ) )
5 FROMEmployeePosition E2
6 [Link] > [Link] );
Q22. Write a query to retrieve duplicate records from a
table.
1SELECTEmpID, EmpFname, Department COUNT(*)
2FROMEmployeeInfo GROUPBYEmpID, EmpFname, Department
3HAVINGCOUNT(*) > 1;
Q23. Write a query to retrieve the list of employees
working in the same department.
[Link], [Link], [Link]
2FROMEmployeeInfo E, Employee E1
[Link] = [Link] [Link] != [Link];
Q24. Write a query to retrieve the last 3 records from the
EmployeeInfo table.
1SELECT* FROMEmployeeInfo WHERE
2EmpID <=3 UNIONSELECT* FROM
3(SELECT* FROMEmployeeInfo E [Link] DESC)
4ASE1 [Link] <=3;
Q25. Write a query to find the third-highest salary from
the EmpPosition table.
1SELECTTOP1 salary
2FROM(
3SELECTTOP3 salary
4FROMemployee_table
5ORDERBYsalary DESC) ASemp
6ORDERBYsalary ASC;
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:
1SELECT* FROMEmployeeInfo WHEREEmpID = (SELECTMIN(EmpID) FROMEmployeeInfo);
To display the last record from the EmployeeInfo table, you can write a query as
follows:
1SELECT* FROMEmployeeInfo WHEREEmpID = (SELECTMAX(EmpID) FROMEmployeeInfo);
Q27. Write a query to add email validation to your
database
1SELECTEmail FROMEmployeeInfo WHERENOTREGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A
Q28. Write a query to retrieve Departments who have less
than 2 employees working in it.
1SELECTDEPARTMENT, COUNT(EmpID) as'EmpNo'FROMEmployeeInfo GROUPBYDEPARTMENT HAVINGCOUNT
Q29. Write a query to retrieve EmpPostion along with total
salaries paid for each of them.
1SELECTEmpPosition, SUM(Salary) fromEmployeePosition GROUPBYEmpPosition;
Q30. Write a query to fetch 50% records from the
EmployeeInfo table.
1SELECT*
2FROMEmployeeInfo WHERE
3EmpID <= (SELECTCOUNT(EmpID)/2 fromEmployeeInfo);