10 Frequently Asked SQL Query Interview Questions - Java67
10 Frequently Asked SQL Query Interview Questions - Java67
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
Java Programming tutorials and Interview Questions
Home
core java
coding
thread
sql
java 8
books
array
string
j2ee
oop
debugging
collections
Categories
data structure
Interview Questions
programming (185)
core java interview question answer
(85)
AM NELES
coding (52)
java (50)
homework (22)
effective way to check candidate's SQL skill is by asking these types of simple query. They are are
neither very complex nor very big, but yet they cover all key concept a programmer should know about
SQL.
These queries test your SQL skill on Joins, both INNER and OUTER join, filtering records by using
WHERE and HAVING clause, grouping records using GROUP BY clause, calculating sum, average
and counting records using aggregate function like AVG(), SUM() and COUNT(), searching records
using wildcards in LIKE operator, searching records in a bound using BETWEEN and IN clause, DATE
and TIME queries etc. If you have faced any interesting SQL query or you have any problem and
searching for the solution, you can post it here for everyone's benefit. If you are looking for more
JSP (10)
challenging SQL query exercises and puzzles then you can also check Joe Cleko's SQL Puzzles And
Answers, one of the best books to really check and improve your SQL skills.
Servlet (10)
java io tutorial (10)
thread interview questions (10)
database (8)
object oriented programming (8)
Eclipse (5)
JDBC (5)
1 of 26
basics (5)
troubleshooting (5)
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
OCAJP (3)
OOPS (3)
Struts (3)
Answer: There are many ways to find second highest salary of Employee in SQL, you can either use
SQL Join or Subquery to solve this problem. Here is SQL query using Subquery:
Recommended Reading
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee 10
);Java Web Service Interview
Questions
debugging (3)
enum (3)
AM NELES
See How to find second highest salary in SQL for more ways to solve this problem.
then using MAX() function to calculate maximum salary in each group or each department.
Followers
GROUP BY DeptID.
Persoane interesate (952) nainte
These questions become more interesting if Interviewer will ask you to print department name instead
of department id, in that case, you need to join Employee table with Department using foreign key
DeptID, make sure you do LEFT or RIGHT OUTER JOIN to include departments without any
employee as well. Here is the query
SELECT DeptName, MAX(Salary) FROM Employee e RIGHT JOIN Department d ON e.DeptId = d.DeptID GROUP
BY DeptName;
Urmrii
Blog Archive
In this query, we have used RIGHT OUTER JOIN because we need the name of the department from
Department table which is on the right side of JOIN clause, even if there is no reference of dept_id on
Employee table.
2016 (126)
2015 (103)
December (8)
November (4)
2 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
October (13)
Question 4: Write an SQL Query to check whether date passed to Query is the date of given
September (10)
format or not.
August (10)
Answer: SQL has IsDate() function which is used to check passed value is a date or not of specified
July (18)
June (22)
and it may not work on Oracle, MySQL or any other database but there would be something similar.
May (5)
April (1)
SELECT
ISDATE('1/08/13') AS "MM/DD/YY";
March (3)
February (2)
January (7)
2014 (67)
2013 (44)
Question 5: Write an SQL Query to print the name of the distinct employee whose DOB is
2012 (122)
Question 6: Write an SQL Query find number of employees according to gender whose DOB is
between 01/01/1960 to 31/12/1975.
Answer :
SELECT COUNT(*), sex from Employees
WHERE
GROUP BY sex;
Question 7: Write an SQL Query to find an employee whose Salary is equal or greater than
10000.
Answer :
SELECT EmpName FROM
Employees WHERE
Salary>=10000;
Question 8: Write an SQL Query to find name of employee whose name Start with M
Answer :
SELECT * FROM Employees WHERE EmpName like 'M%';
3 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
WHERE
Question 10: Write an SQL Query to find the year from date.
Answer: Here is how you can find Year from a Date in SQL Server 2008
SELECT YEAR(GETDATE()) as "Year";
Question 11: Write SQL Query to find duplicate rows in a database? and then write SQL query to
delete them?
Answer: You can use the following query to select distinct records:
SELECT * FROM emp a WHERE rowid = (SELECT MAX(rowid) FROM EMP b WHERE a.empno=b.empno)
to Delete:
DELETE FROM emp a WHERE rowid != (SELECT MAX(rowid) FROM emp b WHERE a.empno=b.empno);
Question 12: There is a table which contains two column Student and Marks, you need to find
all the students, whose marks are greater than average marks i.e. list of above average
students.
Answer: This query can be written using subquery as shown below:
SELECT student, marks from table where marks > SELECT AVG(marks) from table)
4 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Question 13: How do you find all employees which are also manager? .
You have given a standard employee table with an additional column mgr_id, which contains
employee id of the manager.
Answer: You need to know about self-join to solve this problem. In Self Join, you can join two instances
of the same table to find out additional details as shown below
this will show employee name and manager name in two column e.g.
name manager_name
John David
One follow-up is to modify this query to include employees which don't have a manager. To solve that,
instead of using the inner join, just use left outer join, this will also include employees without
managers.
5 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Question 14: You have a composite index of three columns, and you only provide the value of
two columns in WHERE clause of a select query? Will Index be used for this operation? For
example if Index is on EmpId, EmpFirstName, and EmpSecondName and you write query like
If the given two columns are secondary index column then the index will not invoke, but if the given 2
columns contain the primary index(first column while creating index) then the index will invoke. In this
case, Index will be used because EmpId and EmpFirstName are primary columns.
Hope this article will help you to take a quick practice whenever you are going to attend any interview
and not have much time to go into the deep of each query, but if you have good time to prepare then I
suggest you to read and solve SQL queries from Joe Celko's SQL Puzzles and Answers, Second
edition, one of the best book for SQL query lovers and enthusiastic.
6 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
Difference between
Primary and
Foreign key in table
- SQL
How to convert
java.util.Date to
java.sql.Timestamp
in ...
AM NELES
Linkwithin
94 comments:
narasimha May 1, 2013 at 12:13 PM
Really gud bro iam seraching for two months onwards thnxs...
Reply
7 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Replies
Anonymous July 3, 2013 at 3:57 AM
Thanks pal...I needed these things...
8 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
9 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Reply
10 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
select * from emp a where rowid = (select max(rowid) from emp b where a.empno=b.empno)
to Delete:
delete from emp a where rowid != (select max(rowid) from emp b where a.empno=b.empno);
11 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Hi Anonymous, I am planning to add more such SQL queries, but if you have been asked
something then you can also share with us. Thanks
Reply
12 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
13 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
How about the above one. Because an employee is a manager, then he must be having
manager id.
Please correct me if i'm wrong.
Reply
14 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Replies
Anonymous December 10, 2014 at 10:43 PM
Yes, I believe GETDATE() method works only SQL Server, not sure if it works on Oracle or
MySQL. Though in MSSQL it returns :
SELECT GETDATE()
2014-12-11 15:40:02.910
15 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
16 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
17 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
SELECT GetDate();
Many if not most SQL dialects mandate FROM clause.
I am to lazy to read this crap further - if someone would give such answer to me his chances to get the job
will be around zero.
Reply
18 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
19 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
- stored procedure
- and joins again
Good luck
Reply
20 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
21 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
are
Question 1 : list all supplier name and phone where product quantity is 0 ?
Question 2 : list all supplier name where comments is greater then 300 string?
Reply
Replies
Anonymous April 21, 2016 at 7:48 AM
Q1: SELECT s_name,s_phone from Supplier s where s.column_name in (select p.colum_name
from product_detail p where p.p_quantity = 0)
OR
SELECT s_name,s_phone from Supplier s left outer join product_detail p on s.column_name =
p.column_name and p.p_quantity = 0)
22 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
23 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
DA
EA
I want this type output like above.
anybody can help me
Which query is used?
Reply
thanks
Reply
24 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
25 of 26
04.10.2016 11:47
http://www.java67.com/2013/04/10-frequently-asked-sql-query-interview-questions-answers-d...
Acest site folosete cookie-uri pentru a oferi servicii, pentru a personaliza anunuri i pentru a analiza traficul.
Dac folosii acest site, suntei de acord cu utilizarea cookie-urilor.
AM NELES
Comment as:
Publish
Newer Post
Notify me
Home
Older Post
26 of 26
04.10.2016 11:47