SQL Interview Questions
SQL Interview Questions
SQL Interview Questions
Query �
DELETE M1
From managers M1, managers M2
Where M2.Name = M1.Name AND M1.Id>M2.Id;
Output �
ID NAME SALARY
1 Harpreet 20000
2 Ravi 30000
3 Vinay 10000
7 Rajeev 40000
10 Sanjay 50000
Que-3: Find the Name of Employees.
Finding the name of Employees where First Name, Second Name and Last Name is given
in table. Some Name is missing such as First Name, Second Name and may be Last
Name. Here we will use COALESCE() function which will return first Non Null values.
Employees �
ID FNAME SNAME LNAME SALARY
1 Har preet Singh 30000
2 Ashu NULL Rana 50000
3 NULL Vinay Thakur 40000
4 NULL Vinay NULL 10000
5 NULL NULL Rajveer 60000
6 Manjeet Singh NULL 60000
Query �
SELECT ID, COALESCE(FName, SName, LName) as Name
FROM employees;
Output �
Finding the Employees who have been hire in the last n days. Here we get desire
output by using DATEDIFF() mysql function.
Employees �
ID FNAME LNAME GENDER SALARY HIREDATE
1 Rajveer Singh Male 30000 2017/11/05
2 Manveer Singh Male 50000 2017/11/05
3 Ashutosh Kumar Male 40000 2017/12/12
4 Ankita Sharma Female 45000 2017/12/15
5 Vijay Kumar Male 50000 2018/01/12
6 Dilip Yadav Male 25000 2018/02/26
7 Jayvijay Singh Male 30000 2018/02/18
8 Reenu Kumari Female 40000 2017/09/19
9 Ankit Verma Male 25000 2018/04/04
10 Harpreet Singh Male 50000 2017/10/10
Query �
Select *, DATEDIFF (current_date(), Hiredate) as DiffDay
From employees
Where DATEDIFF (current_date(), Hiredate) between 1 and 100 order by Hiredate desc;
Note � Here in query 1 and 100 are indicates 1 to n days.which show the Employees
who have hired last 1 to 100 days. In this query DiffDay is a extra column for our
understanding which show the Nth days.
Output �
Select *
From employees
Where left(FName, 1)='A';
Select *
From employees
Where substring(FName, 1, 1)='A';
Note � Here every query will give same output and the list of Employees who�s FName
start with letter A.
7 Rajeev 40000
10 Sanjay 50000
3. Find the Name of Employees where First Name, Second Name and Last Name is given
in table. Some Name is missing such as First Name, Second Name and may be Last
Name. Here we will use COALESCE() function which will return first Non Null values.
Employees
ID FNAME SNAME LNAME SALARY
1 Har preet Singh 30000
2 Ashu NULL Rana 50000
3 NULL Vinay Thakur 40000
4 NULL Vinay NULL 10000
5 NULL NULL Rajveer 60000
6 Manjeet Singh NULL 60000
Query :
13. Find the Employees who were hired in the Last n years
Finding the Employees who have been hire in the last n years. Here we get desired
output by using TIMESTAMPDIFF() mysql function
Employees