Sunday, July 7, 2019

SQL Query Interview Questions – Practice All Types of SQL Queries

1. Most Asked SQL Query Interview Questions

Earlier we have discussed the different types of SQL questions asked in the interview. Today, we will focus on a particular type and that is SQL Query Interview Questions. So, in this blog, you will find the interview questions based on complex SQL queries that you can practice online. First, we have provided you with a sample table which you will prepare through the SQL queries. Also, we have given how to prepare these SQL tables through different queries. And finally, you will see the most asked SQL Query interview Questions with Answers which will help you to crack your upcoming SQL interview.
So, let’s start the tutorial by creating a table in SQL.
SQL Query Interview Questions - Practice All Type of SQL Queries
SQL Query Interview Questions – Practice All Type of SQL Queries

2. How to Create Table in SQL?

Below are the tables which you will create through the SQL queries.
WORKER_IDFIRST_NAMELAST_NAMESALARYJOINING_DATEDEPARTMENT
001NIHARIKAARORA200002013-02-25 09:00:00HR
002AYUSHIGURONDIA50002015-02-10 09:00:00ADMIN
003PRIYANSHACHOUKSEY250002014-05-16 09:00:00HR
004APARNADESHPANDE80002016-12-20 09:00:00ADMIN
005SHAFALIJAIN210002015-08-29 09:00:00ADMIN
006SUCHITAJOSHI200002017-02-12 09:00:00ACCOUNT
007SHUBHIMISHRA150002018-03-23 09:00:00ADMIN
008DEVYANIPATIDAR180002014-05-02 09:00:00ACCOUNT
TABLE- BONUS
WORKER_REF_IDBONUS_DATEBONUS_AMOUNT
12015-04-20 00:00:005000
22015-08-11 00:00:003000
32015-04-20 00:00:004000
12015-04-20 00:00:004500
22015-08-11 00:00:003500
TABLE- TITLE
WORKER_REF_IDWORKER_TITLEAFFECTED_FROM
1Manager2016-02-20 00:00:00
2Executive2016-06-11 00:00:00
8Executive2016-06-11 00:00:00
5Manager2016-06-11 00:00:00
4Asst. Manager2016-06-11 00:00:00
7Executive2016-06-11 00:00:00
6Lead2016-06-11 00:00:00
3Lead2016-06-11 00:00:00

i. Query to create the Database

  1. CREATE DATABASE ORG;
  2. SHOW DATABASES;
  3. USE ORG;

ii. Query to create the Table

  1. CREATE TABLE Worker (
  2. WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  3. FIRST_NAME CHAR(25),
  4. LAST_NAME CHAR(25),
  5. SALARY INT(15),
  6. JOINING_DATE DATETIME,
  7. DEPARTMENT CHAR(25)
  8. );

iii. Query to insert into the Table Worker

  1. INSERT INTO Worker
  2. (WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT) VALUES
  3. (001, ‘NIHARIKA’, ‘ARORA’, ‘20000’, ‘2013-02-25 09:00:00’, ‘HR’)
  4. (002, ‘AYUSHI’, ‘GURONDIA’, ‘5000’, ‘2015-02-10 09:00:00’, ‘ADMIN’)
  5. (003,’ PRIYANSHA, CHOUKSEY’, ‘25000’, ‘2014-05-16 09:00:00’, ‘HR’)
  6. (004, ‘APARNA’, ’DESHPANDE’, ‘8000’, ‘2016-12-20 09:00:00’, ‘ADMIN’)
  7. (005, ‘SHAFALI’, ‘JAIN’, ‘21000’, ‘2015-08-29 09:00:00’, ADMIN’)
  8. (006, ‘SUCHITA’, ‘JOSHI’, ‘20000’, ‘2017-02-12 09:00:00’, ‘ACCOUNT’)
  9. (007, ‘SHUBHI’, ‘MISHRA’, ‘15000’, ‘2018-03-23 09:00:00’, ‘ADMIN’)
  10. (008, ‘DEVYANI’, ‘PATIDAR’, ‘18000’, ‘2014-05-02 09:00:00’, ‘ACCOUNT’);

iv. Query to create table Bonus

  1. CREATE TABLE Bonus (
  2. WORKER_REF_ID INT,
  3. BONUS_AMOUNT INT(10),
  4. BONUS_DATE DATETIME,
  5. FOREIGN KEY (WORKER_REF_ID)
  6. REFERENCES Worker(WORKER_ID)
  7. ON DELETE CASCADE
  8. );

v. Query to insert into table Bonus

  1. INSERT INTO Bonus
  2. (WORKER_REF_ID, BONUS_AMOUNT, BONUS_DATE) VALUES
  3. (001, 5000, '15-04-20'),
  4. (002, 3000, '15-08-11'),
  5. (003, 4000, '15-04-20'),
  6. (001, 4500, '15-04-20'),
  7. (002, 3500, '15-08-11');

vi. Query to create table Title

  1. CREATE TABLE Title (
  2. WORKER_REF_ID INT,
  3. WORKER_TITLE CHAR(25),
  4. AFFECTED_FROM DATETIME,
  5. FOREIGN KEY (WORKER_REF_ID)
  6. REFERENCES Worker(WORKER_ID)
  7. ON DELETE CASCADE
  8. );

vii. Query to insert into table Title

  1. INSERT INTO Title
  2. (WORKER_REF_ID, WORKER_TITLE, AFFECTED_FROM) VALUES
  3. (001, 'Manager', '2016-02-20 00:00:00'),
  4. (002, 'Executive', '2016-06-11 00:00:00'),
  5. (008, 'Executive', '2016-06-11 00:00:00'),
  6. (005, 'Manager', '2016-06-11 00:00:00'),
  7. (004, 'Asst. Manager', '2016-06-11 00:00:00'),
  8. (007, 'Executive', '2016-06-11 00:00:00'),
  9. (006, 'Lead', '2016-06-11 00:00:00'),
  10. (003, 'Lead', '2016-06-11 00:00:00');
In this way, you can create and insert values into the table. So, let’s start SQL Query Interview Questions and Answers.
Recommended Reading – SQL Subquery

3. 30 Complex SQL Queries Interview Questions and Answers

Now you know how to create table and insert values in it through SQL Query.  So, let’s practice the SQL Query through the best and important SQL Query Interview Questions.
Q.1 Write an SQL query for fetching “FIRST_NAME” from the WORKER table using <WORKER_NAME> as alias.
Ans. The query that you can use is:
  1. Select FIRST_NAME AS WORKER_NAME from Worker;
Q.2 What is an SQL Query for fetching the “FIRST_NAME” from WORKER table in upper case?
Ans. The query that you can use is:
  1. Select upper(FIRST_NAME) from Worker;
Q.3 What is an SQL query for fetching the unique values of the column DEPARTMENT from the WORKER table?
Ans. The query that you cam use is:
  1. Select distinct DEPARTMENT from Worker;
Q.4 Write an SQL query for printing the first three characters of the column FIRST_NAME.
Ans. The query that can be used is:
  1. Select substring(FIRST_NAME,1,3) from Worker;
Q.5 What is an SQL query for finding the position of the alphabet (‘A’) in the FIRST_NAME column of Ayushi.
Ans. The query that can be used is:
  1. Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Ayushi';
Q.6 What is an SQL Query for printing the FIRST_NAME from Worker Table after the removal of white spaces from right side.
Ans. The query that can be used is:
  1. Select RTRIM(FIRST_NAME) from Worker;
Q.7 Write an SQL Query for printing the DEPARTMENT from Worker Table after the removal of white spaces from the left side.
Ans. The query that you can use is:
  1. Select LTRIM(DEPARTMENT) from Worker;
Q.8 What is an SQL query for fetching the unique values from the DEPARTMENT column and thus printing is the length?
Ans. The query that you can use is:
  1. Select distinct length(DEPARTMENT) from Worker;
Q.9 Write an SQL query for printing the FIRST_NAME after replacing ‘A’ with ‘a’.
The query that can be used is:
  1. Select REPLACE(FIRST_NAME,'a','A') from Worker;
Q.10 What is an SQL query for printing the FIRST_NAME and LAST_NAME into a column named COMPLETE_NAME? (A space char should be used)
Ans. The query that can be used is:
  1. Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;
Q.11 What is an SQL query for printing all details of the worker table which ordered by FIRST_NAME ascending?
Ans. The query that can be used is:
  1. Select * from Worker order by FIRST_NAME asc;
Q.12 Write an SQL query for printing the all details of the worker table which ordered by FIRST_NAME ascending and the DEPARTMENT in descending
The query that can be used is:
  1. Select * from Worker order by FIRST_NAME asc,DEPARTMENT desc
Q.13 What is an SQL query to print the details of the workers ‘NIHARIKA’ and ‘PRIYANSHA’.
Ans. The query that can be used is:
  1. Select * from Worker where FIRST_NAME in ('NIHARIKA','PRIYANSHA');
Q.14 What is an SQL query printing all details of workers excluding the first names of ‘NIHARIKA’ and ‘PRIYANSHA’
Ans. The query that can be used is:
  1. Select * from Worker where FIRST_NAME not in ('NIHARIKA','PRYANSHA');
Q.15 Write an SQL query for printing the details of DEPARTMENT name as “Admin”.
Ans. The query that can be used is:
  1. Select * from Worker where DEPARTMENT like 'Admin%';
Q.16  What is an SQL query for printing the details of workers whose FIRST_NAME Contains ‘A’?
Ans. The query that can be used is:
  1. Select * from Worker where FIRST_NAME like '%a%';
Q.17 What is an SQL Query for printing the FIRST_NAME of workers whose name ends with ‘A’?
Ans. The query that can be used is:
  1. Select * from Worker where FIRST_NAME like '%a';
Q.18 What is an SQL Query for printing the details of the workers whose FIRST_NAME ends with ‘H’ and contains six alphabets?
Ans. The query that can be used is:
  1. Select * from Worker where FIRST_NAME like '_____h';
Q.19 Write an SQL Query for printing the details of workers whose SALARY lies between 10000 and 20000.
Ans. The query that can be used is:
  1. Select * from Worker where SALARY between 10000 and 20000;
Q.20 Write an SQL Query for printing the details of workers who joined inFeb’2014
Ans. The query that can be used is:
  1. Select * from Worker where year(JOINING_DATE) = 2014 and month(JOINING_DATE) = 2;
Q.21 Write an SQL Query for fetching the count of workers in DEOARTMENT with ‘Admin’.
Ans. The query that can be used is:
  1. SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';
Q.22 Write an SQL Query for fetching the details of workers with Salaries >= 5000 and <= 10000.
Ans. The query that can be used is:
  1. SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) As Worker_Name, Salary
  2. FROM worker
  3. WHERE WORKER_ID IN
  4. (SELECT WORKER_ID FROM worker
  5. WHERE Salary BETWEEN 5000 AND 10000);
Q.23 What is an SQL Query for fetching the no. of workers in each department in descending order?
Ans. The query that can be used is:
  1. SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers
  2. FROM worker
  3. GROUP BY DEPARTMENT
  4. ORDER BY No_Of_Workers DESC;
Q.24 What is an SQL Query for printing the details of workers who are also managers?
Ans. The query that can be used is:
  1. SELECT DISTINCT W.FIRST_NAME, T.WORKER_TITLE
  2. FROM Worker W
  3. INNER JOIN Title T
  4. ON W.WORKER_ID = T.WORKER_REF_ID
  5. AND T.WORKER_TITLE in ('Manager');
Q.25 Write an SQL Query for fetching the details of duplicate records in some fields.
Ans. The query that can be used is:
  1. SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)
  2. FROM Title
  3. GROUP BY WORKER_TITLE, AFFECTED_FROM
  4. HAVING COUNT(*) > 1;
Q.26 What is an SQL Query for only showing odd rows?
Ans. The query that can be used is:
  1. SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
Q.27 What is an SQL Query for only showing even rows?
Ans. The query that can be used is:
  1. SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) = 0;
Q.28 Write an SQL Query for cloning a new table from another table.
Ans. The general query that can be used to clone a table with data is:
  1. SELECT * INTO WorkerClone FROM Worker;
The general way that can be used to clone a table without information is:
  1. SELECT * INTO WorkerClone FROM Worker WHERE 1 = 0;
Q.29 Write an SQL Query for fetching the intersecting details of two tables.
Ans. The query that can be used is:
  1. (SELECT * FROM Worker)
  2. INTERSECT
  3. (SELECT * FROM WorkerClone);
Q.30 What is an SQL Query for showing the details of one table that another doesn’t have.
Ans. The query that can be used is:
  1. SELECT * FROM Worker
  2. MINUS
  3. SELECT * FROM Title;
So, this was all in SQL Query Interview Questions and Answers. Hope you liked the explanation.

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...