SQL Interview Questions | Set 2

14 Jun, 2022
Difference between Locking, Blocking and DeadlockingLocking: Locking occurs when a connection needs access to a piece of data in a database and it locks it for certain use so that no other transaction is able to access it.Blocking: Blocking occurs when a transaction tries to acquire an incompatible lock on a resource that another transaction has already locked. The blocked transaction remains blocked until the blocking transaction releases the lock.Deadlocking: Deadlocking occurs when two or more transactions have a resource locked, and each transaction requests a lock on the resource that another transaction has already locked. Neither of the transactions here can move forward, as each one is waiting for the other to release the lock.Delete duplicate data from table so that only first data remains constantManagersIdNameSalary1Harpreet200002Ravi300003Vinay100004Ravi300005Harpreet200006Vinay100007Rajeev400008Vinay100009Ravi3000010Sanjay50000Query:DELETE M1 from managers M1, managers M2 where M2.Name=M1.Name AND M1.Id>M2.Id;
Output:IdNameSalary1Harpreet200002Ravi300003Vinay100007Rajeev4000010Sanjay50000Find the Name of Employees where First Name, Second Name, and Last Name is given in the table. Some Name is missing such as First Name, Second Name and maybe Last Name. Here we will use COALESCE() function which will return first Non Null values.EmployeesIDFNameSNameLNameSalary1HarpreetSingh300002AshuNULLRana500003NULLVinayThakur400004NULLVinayNULL100005NULLNULLRajveer600006ManjeetSinghNULL60000Query :SELECT ID, COALESCE(FName, SName, LName) as Name FROM employees;
Output:Find the Employees who were hired in the Last n monthsFinding the Employees who have been hire in the last n months. Here we get desired output by using TIMESTAMPDIFF() mysql functionEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002017/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002017/12/124AnkitaSharmaFemale450002017/12/155VijayKumarMale500002018/01/126DilipYadavMale250002018/02/267JayvijaySinghMale300002018/02/188ReenuKumariFemale400002017/09/199AnkitVermaMale250002018/04/0410HarpreetSinghMale500002017/10/10Query:Select *, TIMESTAMPDIFF(month, Hiredate, current_date()) as 
DiffMonth from employees
where TIMESTAMPDIFF(month, Hiredate, current_date()) between 
1 and 5 order by Hiredate desc;
Note: Here in query 1 and 5 are indicates 1 to n months which show the Employees who have hired last 1 to 5 months. In this query, DiffMonth is an extra column for our understanding which shows the Nth months.Output:Find the Employees who hired in the Last n daysFinding the Employees who have been hired in the last n days. Here we get desired output by using DATEDIFF() mysql functionEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002017/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002017/12/124AnkitaSharmaFemale450002017/12/155VijayKumarMale500002018/01/126DilipYadavMale250002018/02/267JayvijaySinghMale300002018/02/188ReenuKumariFemale400002017/09/199AnkitVermaMale250002018/04/0410HarpreetSinghMale500002017/10/10Query: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 indicates 1 to n days which show the Employees who have hired last 1 to 100 days. In this query DiffDay is an extra column for our understanding which shows the Nth days.Output:Find the Employees who were hired in the Last n yearsFinding the Employees who have been hired in the last n years. Here we get desired output by using TIMESTAMPDIFF() MySQL functionEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002010/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002015/12/124AnkitaSharmaFemale450002016/12/155VijayKumarMale500002017/01/126DilipYadavMale250002011/02/267JayvijaySinghMale300002012/02/188ReenuKumariFemale400002013/09/199AnkitVermaMale250002017/04/0410HarpreetSinghMale500002017/10/10Query:select *, TIMESTAMPDIFF(year, Hiredate, current_date()) as 
DiffYear from employees
where TIMESTAMPDIFF(year, Hiredate, current_date()) 
between 1 and 4 order by Hiredate desc;
Note: Here in query 1 and 4 are indicates 1 to n years which shows the Employees who have hired last 1 to 4 years. In this query, DiffYear is a extra column for our understanding which show the Nth years.Output:Select all names that start with a given letterHere we get desired output by using three different queriesEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002010/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002015/12/124AnkitaSharmaFemale450002016/12/155VijayKumarMale500002017/01/126DilipYadavMale250002011/02/267JayvijaySinghMale300002012/02/188ReenuKumariFemale400002013/09/199AnkitVermaMale250002017/04/0410HarpreetSinghMale500002017/10/10Query:select *from employees where Fname like 'A%';

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.
Difference between Locking, Blocking and DeadlockingLocking: Locking occurs when a connection needs access to a piece of data in a database and it locks it for certain use so that no other transaction is able to access it.Blocking: Blocking occurs when a transaction tries to acquire an incompatible lock on a resource that another transaction has already locked. The blocked transaction remains blocked until the blocking transaction releases the lock.Deadlocking: Deadlocking occurs when two or more transactions have a resource locked, and each transaction requests a lock on the resource that another transaction has already locked. Neither of the transactions here can move forward, as each one is waiting for the other to release the lock.
Locking: Locking occurs when a connection needs access to a piece of data in a database and it locks it for certain use so that no other transaction is able to access it.
Blocking: Blocking occurs when a transaction tries to acquire an incompatible lock on a resource that another transaction has already locked. The blocked transaction remains blocked until the blocking transaction releases the lock.
Deadlocking: Deadlocking occurs when two or more transactions have a resource locked, and each transaction requests a lock on the resource that another transaction has already locked. Neither of the transactions here can move forward, as each one is waiting for the other to release the lock.
Delete duplicate data from table so that only first data remains constantManagersIdNameSalary1Harpreet200002Ravi300003Vinay100004Ravi300005Harpreet200006Vinay100007Rajeev400008Vinay100009Ravi3000010Sanjay50000Query:DELETE M1 from managers M1, managers M2 where M2.Name=M1.Name AND M1.Id>M2.Id;
Output:IdNameSalary1Harpreet200002Ravi300003Vinay100007Rajeev4000010Sanjay50000
Managers
Query:
DELETE M1 from managers M1, managers M2 where M2.Name=M1.Name AND M1.Id>M2.Id;

Output:
Find the Name of Employees where First Name, Second Name, and Last Name is given in the table. Some Name is missing such as First Name, Second Name and maybe Last Name. Here we will use COALESCE() function which will return first Non Null values.EmployeesIDFNameSNameLNameSalary1HarpreetSingh300002AshuNULLRana500003NULLVinayThakur400004NULLVinayNULL100005NULLNULLRajveer600006ManjeetSinghNULL60000Query :SELECT ID, COALESCE(FName, SName, LName) as Name FROM employees;
Output:
Employees
Query :
SELECT ID, COALESCE(FName, SName, LName) as Name FROM employees;

Output:
Find the Employees who were hired in the Last n monthsFinding the Employees who have been hire in the last n months. Here we get desired output by using TIMESTAMPDIFF() mysql functionEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002017/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002017/12/124AnkitaSharmaFemale450002017/12/155VijayKumarMale500002018/01/126DilipYadavMale250002018/02/267JayvijaySinghMale300002018/02/188ReenuKumariFemale400002017/09/199AnkitVermaMale250002018/04/0410HarpreetSinghMale500002017/10/10Query:Select *, TIMESTAMPDIFF(month, Hiredate, current_date()) as 
DiffMonth from employees
where TIMESTAMPDIFF(month, Hiredate, current_date()) between 
1 and 5 order by Hiredate desc;
Note: Here in query 1 and 5 are indicates 1 to n months which show the Employees who have hired last 1 to 5 months. In this query, DiffMonth is an extra column for our understanding which shows the Nth months.Output:
Query:
Select *, TIMESTAMPDIFF(month, Hiredate, current_date()) as 
DiffMonth from employees
where TIMESTAMPDIFF(month, Hiredate, current_date()) between 
1 and 5 order by Hiredate desc;

Note: Here in query 1 and 5 are indicates 1 to n months which show the Employees who have hired last 1 to 5 months. In this query, DiffMonth is an extra column for our understanding which shows the Nth months.
Output:
Find the Employees who hired in the Last n daysFinding the Employees who have been hired in the last n days. Here we get desired output by using DATEDIFF() mysql functionEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002017/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002017/12/124AnkitaSharmaFemale450002017/12/155VijayKumarMale500002018/01/126DilipYadavMale250002018/02/267JayvijaySinghMale300002018/02/188ReenuKumariFemale400002017/09/199AnkitVermaMale250002018/04/0410HarpreetSinghMale500002017/10/10Query: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 indicates 1 to n days which show the Employees who have hired last 1 to 100 days. In this query DiffDay is an extra column for our understanding which shows the Nth days.Output:
Employees
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 indicates 1 to n days which show the Employees who have hired last 1 to 100 days. In this query DiffDay is an extra column for our understanding which shows the Nth days.
Output:
Find the Employees who were hired in the Last n yearsFinding the Employees who have been hired in the last n years. Here we get desired output by using TIMESTAMPDIFF() MySQL functionEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002010/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002015/12/124AnkitaSharmaFemale450002016/12/155VijayKumarMale500002017/01/126DilipYadavMale250002011/02/267JayvijaySinghMale300002012/02/188ReenuKumariFemale400002013/09/199AnkitVermaMale250002017/04/0410HarpreetSinghMale500002017/10/10Query:select *, TIMESTAMPDIFF(year, Hiredate, current_date()) as 
DiffYear from employees
where TIMESTAMPDIFF(year, Hiredate, current_date()) 
between 1 and 4 order by Hiredate desc;
Note: Here in query 1 and 4 are indicates 1 to n years which shows the Employees who have hired last 1 to 4 years. In this query, DiffYear is a extra column for our understanding which show the Nth years.Output:
Employees
Query:
select *, TIMESTAMPDIFF(year, Hiredate, current_date()) as 
DiffYear from employees
where TIMESTAMPDIFF(year, Hiredate, current_date()) 
between 1 and 4 order by Hiredate desc;

Note: Here in query 1 and 4 are indicates 1 to n years which shows the Employees who have hired last 1 to 4 years. In this query, DiffYear is a extra column for our understanding which show the Nth years.
Output:
Select all names that start with a given letterHere we get desired output by using three different queriesEmployeesIDFNameLNameGenderSalaryHiredate1RajveerSinghMale300002010/11/052ManveerSinghMale500002017/11/053AshutoshKumarMale400002015/12/124AnkitaSharmaFemale450002016/12/155VijayKumarMale500002017/01/126DilipYadavMale250002011/02/267JayvijaySinghMale300002012/02/188ReenuKumariFemale400002013/09/199AnkitVermaMale250002017/04/0410HarpreetSinghMale500002017/10/10Query:select *from employees where Fname like 'A%';

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.
Employees
Query:
select *from employees where Fname like 'A%';

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.
Related articles :
SQL Interview Questions | Set 1Commonly asked DBMS Questions Set 1Commonly asked DBMS Questions Set 2
SQL Interview Questions | Set 1
Commonly asked DBMS Questions Set 1
Commonly asked DBMS Questions Set 2
zeto
vinayedula
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
Difference between Clustered and Non-clustered index
Data Preprocessing in Data Mining
Difference between DELETE, DROP and TRUNCATE
Difference between SQL and NoSQL
SQL | DDL, DQL, DML, DCL and TCL Commands
How to find Nth highest salary from a table
CTE in SQL
SQL | ALTER (RENAME)
How to Update Multiple Columns in Single Update Statement in SQL?