3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
Home About Us Privacy Policy Contact Us
SQL WORLD
PA R S E, BIND, OPTIMIZ E , E X E C U T E
Complex SQL Queries
Following are some very important Complex SQL Queries
Examples with answers.I have tried to explain each and every
query in detail so that everyone will get idea of how it is executed
[Link] are some Complex SQL Queries
Examples with answers in detail.
Complex SQL Queries Examples ( 90%
ASKED IN Interviews )
[Link] to nd Second Highest Salary of Employee?(click for
explaination)
Answer:
Select distinct Salary from Employee e1 where
2=Select count(distinct Salary) from Employee e2
where [Link]<=[Link];
[Link] 1/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
Alternative Solution : Suggested by Ankit Srivastava
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
select min(salary)from(select distinct salary from emp
order by salary desc)where rownum<=2;
[Link] to nd duplicate rows in table?(click here for
explaination )
Answer :
Select * from Employee a where rowid <>( select
max(rowid) from Employee b where
a.Employee_num=b.Employee_num);
[Link] to fetch monthly Salary of Employee if annual salary is
given?(click here for Explaination)
8 New Cars So Hotels in Berlin Might
Awesome It's Hard to Actually Surprise You
Believe They Cost
Under $20k
Answer:
Select Employee_name,Salary/12 as ‘Monthly Salary’
from employee;
Click here to get information on ROW_ID
[Link] is the Query to fetch rst record from Employee table?
(90% asked Complex SQL Queries Examples)
Answer:
[Link] 2/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
Select * from Employee where Rownum =1;
Click here to get What is Rownum?
[Link] is the Query to fetch last record from the table?
Answer:
Select * from Employee where Rowid= select
max(Rowid) from Employee;
Complex SQL Queries
Click here to get 20 interview questions on Perforance Tuning..
[Link] is Query to display rst 5 Records from Employee table?
(90% asked Complex SQL Queries Examples)
Answer:
Select * from Employee where Rownum <= 5;
[Link] 3/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
HOME
SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
CLICK HERE TO GET INFORMATION ON NORMALIZATION
[Link] is Query to display last 5 Records from Employee table?
(90% asked Complex SQL Queries Examples)
Answer:
Select * from Employee e where rownum <=5
union
select * from (Select * from Employee e order by rowid
desc) where rownum <=5;
Click Here to get What is Union?
[Link] is Query to display Nth Record from Employee table?
Answer :
select * from ( select a.*, rownum rnum from (
YOUR_QUERY_GOES_HERE — including the order by ) a
where rownum <= N_ROWS ) where rnum >= N_ROWS
[Link] to get 3 Highest salaries records from Employee table?
Answer:
select distinct salary from employee a where 3 >= (select
count(distinct salary) from employee b where [Link] <=
[Link]) order by [Link] desc;
Alternative Solution: Suggested by Ankit Srivastava
select min(salary)from(select distinct salary from emp
order by salary desc)where rownum<=3;
[Link] 4/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
[Link] to Display Odd rows in Employee table?(90% asked
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
Complex SQL Queries Examples)
Answer:
Select * from(Select rownum as rno,E.* from Employee E)
where Mod(rno,2)=1;
[Link] to Display Even rows in Employee table?
Answer:
Select * from(Select rownum as rno,E.* from Employee)
where Mod(rno,2)=0;
Learn SQL Server Course here: SQL
Server Training
[Link] to fetch 3rd highest salary using Rank Function?
Answer:
select * from (Select Dense_Rank() over ( order by salary
desc) as Rnk,E.* from Employee E) where Rnk=3;
Click Here to Get Information on Rank and Dense_Rank
[Link] Can i create table with same structure of Employee
table?(90% asked Complex SQL Queries Examples)
Answer:
SPONSORED SEARCHES
Complex SQL Queries
SQL Practice
Create table Employee_1 as Select * from Employee
where 1=2;
[Link] rst 50% records from Employee table?
[Link] 5/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
Answer:
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
select rownum, e.* from emp e where rownum<=(select
count(*)/2 from emp);
[Link] last 50% records from Employee table?
Answer:
Select rownum,E.* from Employee E
minus
Select rownum,E.* from Employee E where rownum<=
(Select count(*)/2) from Employee);
[Link] Can i create table with same structure with data of
Employee table?
Answer:
Create table Employee1 as select * from Employee;
[Link] do i fetch only common records between 2 tables.
Answer:
Select * from Employee;
Intersect
Select * from Employee1;
CLICK HERE TO GET INFORMATION ABOUT INTERSECT
OPERATOR
[Link] Query to get information of Employee where Employee
is not assigned to the department
Answer:
[Link] 6/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
Select
HOME * from Employee
SQL QUERIESwhere Dept_noORACLE
ORACLE Not in(Select
BI UNIX POSTGRESQL INTERVIEW Q/A
Department_no from Department);
[Link] to get distinct records from the table without using
distinct keyword.
Answer:
select * from Employee a where rowid = (select
max(rowid) from Employee b where
a.Employee_no=b.Employee_no);
[Link] all records from Employee table whose name is ‘Amit’
and ‘Pradnya’
Answer:
Select * from Employee where Name in(‘Amit’,’Pradnya’);
[Link] all records from Employee table where name not in
‘Amit’ and ‘Pradnya’
Answer:
select * from Employee where name Not in
(‘Amit’,’Pradnya’);
Click Here to get 20 Interview Questions for Tech Mahindra….
[Link] to write sql query for the below scenario
I/p:ORACLE
O/p:
O
R
A
C
L
E
i.e, splitting into multiple columns a string using sql.
[Link] 7/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
Answer:
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
Select Substr(‘ORACLE’,Level,1) From Dual
Connect By Level<= Length(‘ORACLE’);
[Link] to fetch all the records from Employee whose joining
year is 2017?
Answer:
Oracle:
select * from Employee where
To_char(Joining_date,’YYYY’)=’2017′;
MS SQL:
select * from Employee where
substr(convert(varchar,Joining_date,103),7,4)=’2017′;
[Link] is SQL Query to nd maximum salary of each
department?
Answer:
Select Dept_id,max(salary) from Employee group by
Dept_id;
[Link] Do you nd all Employees with its managers?(Consider
there is manager id also in Employee table)
Answer:
Select e.employee_name,[Link] name from
Employee e,Employee m where
e.Employee_id=m.Manager_id;
[Link] the name of employees who have joined in 2016 and
salary is greater than 10000?
[Link] 8/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
Answer:
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
Select name from Employee where Hire_Date like
‘2016%’ and salary>10000;
[Link] to display following using query?
**
***
Answer:
We cannot use dual table to display output given above. To
display output use any table. I am using Student table.
SELECT lpad (‘*’, ROWNUM,’*’) FROM Student WHERE
ROWNUM <4;
[Link] to add the email validation using only one query?
Answer :
User needs to use REGEXP_LIKE function for email validation.
SELECT
Email
FROM
Employee
where NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-
Z0-9.-]+\.[A-Z]{2,4}’, ‘i’);
[Link] to display 1 to 100 Numbers with query?
Answer:
Select level from dual connect by level <=100;
[Link] 9/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
Tip: User needs to know the concept of Hierarchical queries.
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
Click here to get concept of hierarchical queries
[Link] to remove duplicate rows from table?(100% asked
in Complex SQL Queries for Interviews)
Answer:
First Step: Selecting Duplicate rows from table
Tip: Use concept of max (rowid) of table. Click here to get
concept of rowid.
Select rollno FROM Student WHERE ROWID <>
(Select max (rowid) from Student b where
rollno=[Link]);
Step 2: Delete duplicate rows
Delete FROM Student WHERE ROWID <>
(Select max (rowid) from Student b where
rollno=[Link]);
[Link] to nd count of duplicate rows? (95% asked in SQL
queries for Interviews )
Answer:
Select rollno, count (rollno) from Student
Group by rollno
Having count (rollno)>1
Order by count (rollno) desc;
[Link] to Find the Joining date of Employee in YYYY-DAY-
Date format.
[Link] 10/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
HOME
Select SQL QUERIES
FIRST_NAME, ORACLE ORACLE BI
to_char(joining_date,’YYYY’) UNIX POSTGRESQL INTERVIEW Q/A
JoinYear , to_char(joining_date,’Mon’),
to_char(joining_date,’dd’) from EMPLOYEES;
Hope This article named Complex SQL queries examples is useful
to all the programmers. This article gives you the idea about
Complex SQL Queries examples and will be useful to all the
programmers.
Register for free and Get SQL
Interview Questions PDF :
FOR ISSUES CONTACT :
compexsql@[Link]
[Link] 11/12
3/29/2020 Complex SQL Queries | Complex SQL Queries Examples
HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A
[Link] 12/12