SQL Assignment
SQL Assignment
Connected.
JOINING_DATE
---------------------------------------------------------------------------
DEPARTMENT
----------
01-JAN-13 12.00.00.000000 AM
Banking
01-JAN-13 12.00.00.000000 AM
Insurance
JOINING_DATE
---------------------------------------------------------------------------
DEPARTMENT
----------
01-FEB-13 12.00.00.000000 AM
Banking
01-FEB-13 12.00.00.000000 AM
JOINING_DATE
---------------------------------------------------------------------------
DEPARTMENT
----------
Insurance
01-FEB-13 12.00.00.000000 AM
Insurance
---------------------------------------------------------------------------
DEPARTMENT
----------
01-JAN-13 12.00.00.000000 AM
Services
01-FEB-13 12.00.00.000000 AM
Insurance
JOINING_DATE
---------------------------------------------------------------------------
DEPARTMENT
----------
01-JAN-13 12.00.00.000000 AM
SERVICES
8 rows selected.
DEPARTMENT
8 rows selected.
no rows selected
SQL> create table Incentives2(employee_ref_id number(10), incentive_date date,
incentive_amount number(10));
ERROR at line 1:
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Name
Null? Type
-----------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------ --------
-----------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------
EMPLOYEE_REF_ID
NUMBER(10)
INCENTIVE_DATE
DATE
INCENTIVE_AMOUNT
NUMBER(10)
1 01-FEB-13 5000
2 01-FEB-13 3000
3 01-FEB-13 4000
1 01-JAN-13 4500
2 01-JAN-13 3500
DEPARTMENT
FIRST_NAME LAST_NAME
-------------------- --------------------
John Abraham
Michael Clark
Roy Thomas
Tom Jose
Jerry Pinto
Testname1 123
Testname Lname%
Philip Mathew
8 rows selected.
EMPLOYEE_NAME
--------------------
John
Michael
Roy
Tom
Jerry
Testname1
Testname
Philip
8 rows selected.
UPPER(FIRST_NAME)
--------------------
JOHN
MICHAEL
ROY
TOM
JERRY
TESTNAME1
TESTNAME
PHILIP
8 rows selected.
LOWER(FIRST_NAME)
--------------------
john
michael
roy
tom
jerry
testname1
testname
philip
8 rows selected.
DEPARTMENT
----------
Insurance
SERVICES
Banking
Services
SUBSTR(FIRST
------------
Joh
Mic
Roy
Tom
Jer
Tes
Tes
Phi
8 rows selected.
ERROR at line 1:
no rows selected
ERROR at line 1:
ERROR at line 1:
RTRIM(FIRST_NAME)
--------------------
John
Michael
Roy
Tom
Jerry
Testname1
Testname
Philip
8 rows selected.
LTRIM(FIRST_NAME)
--------------------
John
Michael
Roy
Tom
Jerry
Testname1
Testname
Philip
8 rows selected.
LENGTH(FIRST_NAME)
------------------
8 rows selected.
SQL> SELECT LEN(FIRST_NAME) FROM EMPLOYEE;
ERROR at line 1:
REPLACE(FIRST_NAME,'
--------------------
John
Michael
Roy
Tom
Jerry
Testname1
Testname
Philip
8 rows selected.
8 rows selected.
FIRST_NAME||'_'||LAST_NAME
-----------------------------------------
John_Abraham
Michael_Clark
Roy_Thomas
Tom_Jose
Jerry_Pinto
Testname1_123
Testname_Lname%
Philip_Mathew
8 rows selected.
ERROR at line 1:
ERROR at line 1:
*
ERROR at line 1:
DEPARTMENT
8 rows selected.
ERROR at line 1:
DEPARTMENT
8 rows selected.
DEPARTMENT
8 rows selected.
no rows selected
DEPARTMENT
DEPARTMENT
7 rows selected.
no rows selected
DEPARTMENT
DEPARTMENT
no rows selected
ERROR at line 1:
ERROR at line 1:
DEPARTMENT
6 rows selected.
DEPARTMENT
SQL> select * from EMPLOYEE WHERE SALARY BETWEEN 500000 AND 800000;
DEPARTMENT
7 rows selected.
no rows selected
DEPARTMENT
DEPARTMENT
ERROR at line 1:
ERROR:
SQL>
*
ERROR at line 1:
ERROR at line 1:
ERROR at line 1:
SQL>
[12/20, 1:00 PM] Sagar thakare: Table Name : Employee EMPLOYEE_ID FIRST_NAME
LAST_NAME SALARY JOINING_DATE 1
John
Abraham
DEPARTMENT
Michael
Clarke
800000
Roy
Thomas
700000
01-FEB-13
Banking
12.00.00 AM 4
Tom
Jose
600000
01-FEB-13
Insurance
12.00.00 AM 5
Jerry
Pinto
650000
01-FEB-13
Insurance
12.00.00 AM 6
Philip
Mathew
750000
TestName1
123
650000
TestName2
Lname%
600000
01-FEB-13 12.00.00 AM
01-FEB-13
5000
01-FEB-13
3000
01-FEB-13
4000
01-JAN-13
4500
01-JAN-13
3500
Insurance
SQL Queries Interview Questions and Answers on "SQL Select" - Examples 1. Get all employee
details from the employee table Select * from employee
3. Get First_Name from employee table using alias name “Employee Name” Select first_name
Employee Name from employee
4. Get First_Name from employee table in upper case Select upper(FIRST_NAME) from
EMPLOYEE
5. Get First_Name from employee table in lower case Select lower(FIRST_NAME) from
EMPLOYEE
6. Get unique DEPARTMENT from employee table select distinct DEPARTMENT from
EMPLOYEE
7. Select first 3 characters of FIRST_NAME from EMPLOYEE Oracle
of
SQL Server
substr(FIRST_NAME,0,3)
SQL Server
Equivalent of Oracle
substring(FIRST_NAME,0,3)
SUBSTRING
is
SUBSTR,
from
Query :
select
employee
Query :
select
employee
[12/20, 1:02 PM] Sagar thakare: MySQL Server Equivalent of Oracle SUBSTR is SUBSTRING. In
MySQL start position is 1, Query : select substring(FIRST_NAME,1,3) from employee
Oracle
Equivalent
of
instr(FIRST_NAME,'o')
SQL
Server
Equivalent
SQL
Server
from
of
employee
Oracle
CHARINDEX('o',FIRST_NAME,0)
MySQL
Server
Equivalent
CHARINDEX
from
of
INSTR,
where
INSTR
is
employee
Oracle
is
first_name
CHARINDEX,
where
INSTR
Query
is
Select 'John'
Query:
first_name
LOCATE,
Select
'John'
Query
Select
10. Get FIRST_NAME from employee table after removing white spaces from left side select
LTRIM(FIRST_NAME) from employee
Equivalent of
SQL Server
length(FIRST_NAME)
SQL
Server
Len is
Length
, Query
from
Equivalent
of
Oracle,MYSQL
:select employee
Length
is
Len,
Query
:select
12. Get First_Name from employee table after replacing 'o' with '$' select
REPLACE(FIRST_NAME,'o','$') from employee
13. Get First_Name and Last_Name as single column from employee table separated by a ''
Oracle Equivalent of MySQL concat is '||', Query : Select FIRST_NAME|| ''
||LAST_NAME
from
EMPLOYEE
SQL Server Equivalent of MySQL concat is '+', Query : Select FIRST_NAME + '_'
MySQL
+LAST_NAME
Equivalent
of
from
Oracle
'||'
is
EMPLOYEE
concat,
Query
Select
14. Get FIRST_NAME ,Joining year,Joining Month and Joining Date from employee table SQL
Queries in Oracle, Select FIRST_NAME, to_char(joining_date,'YYYY') JoinYear
to_char(joining_date,'Mon'),
to_char(joining_date,'dd')
from
EMPLOYEE
SQL
Queries
in
SQL
Server,
select
SUBSTRING
(convert(varchar,joining_date,103),7,4)
SUBSTRING
(convert(varchar,joining_date,100),1,3)
SUBSTRING
(convert(varchar,joining_date,100),5,2)
SQL
Queries
in
MySQL,
select
from
EMPLOYEE
year(joining_date),month(joining_date),
[12/20, 1:05 PM] Sagar thakare: 15. Get all employee details from the employee table order by
First_Name Ascending Select * from employee order by FIRST_NAME asc 16. Get all employee
details from the employee table order by First_Name descending Select * from employee order
by FIRST_NAME desc 17. Get all employee details from the employee table order by
First_Name Ascending and Salary descending
SQL Queries Interview Questions and Answers on "SQL Where Condition" - Examples 18. Get
employee details from employee table whose employee name is “John” Select * from
EMPLOYEE where FIRST_NAME = 'John'
19. Get employee details from employee table whose employee name are “John” and “Roy”
Select * from EMPLOYEE where FIRST_NAME in ('John','Roy')
20. Get employee details from employee table whose employee name are not “John” and
“Roy” Select * from EMPLOYEE where FIRST_NAME not in ('John','Roy')
SQL Queries Interview Questions and Answers on "SQL Wild Card Search" - Examples 21. Get
employee details from employee table whose first name starts with 'J' Select * from
EMPLOYEE where FIRST_NAME like 'J%'
22. Get employee details from employee table whose first name contains 'o' Select * from
EMPLOYEE where FIRST_NAME like '%o%'
23. Get employee details from employee table whose first name ends with 'n' Select * from
EMPLOYEE where FIRST_NAME like '%n'
Questions
and
Answers on
"SQL
Pattern
[12/20, 1:06 PM] Sagar thakare: 24. Get employee details from employee table whose first
name ends with 'n' and name contains 4 letters Select * from EMPLOYEE where FIRST_NAME
like '___n' (Underscores)
25. Get employee details from employee table whose first name starts with 'J' and name
contains 4 letters Select * from EMPLOYEE where FIRST_NAME like 'J___' (Underscores)
26. Get employee details from employee table whose Salary greater than 600000 Select * from
EMPLOYEE where Salary > 600000
27. Get employee details from employee table whose Salary less than 800000 Select * from
EMPLOYEE where Salary < 800000
28. Get employee details from employee table whose Salary between 500000 and 800000
Select * from EMPLOYEE where Salary between 500000 and 800000
29. Get employee details from employee table whose name is 'John' and 'Michael' Select *
from EMPLOYEE where FIRST_NAME in ('John','Michael')
SQL Queries Interview Questions and Answers on "SQL DATE Functions" Examples 30. Get
employee details from employee table whose joining year is “2013” SQL Queries in Oracle,
Select * from EMPLOYEE where to_char(joining_date,'YYYY') = '2013'
Queries
in
SQL
Server,
Select
from
SUBSTRING(convert(varchar,joining_date,103),7,4)
where '2013'
31. Get employee details from employee table whose joining month is “January” SQL
Queries
in
Oracle,
to_char(joining_date,'MM')
Select '01'
to_char(joining_date,'Mon') = 'Jan'
or
* Select
from *
from
EMPLOYEE EMPLOYEE
where where
SQL
Queries
in
SQL
Select
*
from
EMPLOYEE
where
SUBSTRING(convert(varchar,joining_date,100),1,3) = 'Jan'
32. Get employee details from employee table who joined before January 1st 2013 SQL
Queries
in
Oracle,
Select
from
EMPLOYEE
where
JOINING_DATE
'2013-01-31'
EMPLOYEE
EMPLOYEE
from
EMPLOYEE
Queries
in
Oracle,
select
to_char(JOINING_DATE,'dd/mm/yyyy
SQL
Queries
in
MySQL,
Select
MICROSECOND(joining_date)
from
EMPLOYEE
[12/20, 1:11 PM] Sagar thakare: 37. Get difference between JOINING_DATE and
INCENTIVE_DATE from employee and incentives table Select
FIRST_NAME,INCENTIVE_DATE
JOINING_DATE
from
employee
inner
39. Get names of employees from employee table who has '%' in Last_Name. Tip : Escape
character for special characters in a query.
SQL Queries in Oracle, Select FIRST_NAME from employee where Last_Name like '%?%%' SQL
Queries in SQL Server, Select FIRST_NAME from employee where Last_Name like '%[%]%' SQL
Queries in MySQL,Select FIRST_NAME from employee where Last_Name like '%\%%' 40. Get
Last Name from employee table after replacing special character with white space SQL
Queries in Oracle, Select translate(LAST_NAME,'%',' ') from employee
SQL Queries in SQL Server and MySQL, Select REPLACE(LAST_NAME,'%',' ') from employee
Questions and
Answers on
"SQL
Group
By
41. Get department,total salary with respect to a department from employee table. Select
DEPARTMENT,sum(SALARY)
Total_Salary
from
employee
group
by
department
42. Get department,total salary with respect to a department from employee table order by
total salary descending Select
DEPARTMENT,sum(SALARY)
Total_Salary
from
employee
group
by
SQL Queries Interview Questions and Answers on "SQL Mathematical Operations using Group
By" - Examples 43. Get department,no of employees in a department,total salary with respect
to a department from employee table order by total salary descending Select
DEPARTMENT,count(FIRST_NAME),sum(SALARY)
Total_Salary
from
[12/20, 1:12 PM] Sagar thakare: 44. Get department wise average salary from employee table
order by salary ascending select DEPARTMENT,avg(SALARY) AvgSalary from employee group
by DEPARTMENT order by AvgSalary asc
45. Get department wise maximum salary from employee table order by salary ascending
select DEPARTMENT,max(SALARY) MaxSalary from employee group by DEPARTMENT order by
MaxSalary asc
46. Get department wise minimum salary from employee table order by salary ascending
select DEPARTMENT,min(SALARY) MinSalary from employee group by DEPARTMENT order by
MinSalary asc
47. Select no of employees joined with respect to year and month from employee table SQL
Queries
in
Oracle,
select
to_char
(JOINING_DATE,'YYYY')
group
by
to_char
(JOINING_DATE,'YYYY'),to_char(JOINING_DATE,'MM')
SQL
Queries
in
Join_Year,datepart
SQL
Server,
select
(MM,JOINING_DATE)
datepart
(YYYY,JOINING_DATE)
Join_Month,count(*)
Total_Emp
from
SQL
Queries
(JOINING_DATE)
in
MySQL,
select
Join_Month,count(*)
year(JOINING_DATE),
year
(JOINING_DATE)
Total_Emp
from
Join_Year,month
employee
group
by
month(JOINING_DATE)
DEPARTMENT,sum(SALARY)
Total_Salary
from
employee
group
by
49. Select first_name, incentive amount from employee and incentives table for those
employees who have incentives Select FIRST_NAME,INCENTIVE_AMOUNT from employee a
inner join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID 50. Select first_name,
incentive amount from employee and incentives table for those employees who have
incentives and incentive amount greater than 3000 Select FIRST_NAME,INCENTIVE_AMOUNT
from employee a inner join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID and
INCENTIVE_AMOUNT > 3000
51. Select first_name, incentive amount from employee and incentives table for all employes
even if they didn't get incentives Select FIRST_NAME,INCENTIVE_AMOUNT from employee a
left join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID
52. Select first_name, incentive amount from employee and incentives table for all employees
even if they didn't get incentives and set incentive amount as 0 for those employees who
didn't get incentives. SQL
Queries
in
Oracle,
Select
FIRST_NAME,nvl(INCENTIVE_AMOUNT,0)
from
employee
left
join
incentives
on
A.EMPLOYEE_ID
B.EMPLOYEE_REF_ID
[12/20, 1:14 PM] Sagar thakare: 53. Select first_name, incentive amount from employee and
incentives table for all employees who got incentives using left join SQL
Queries
in
Oracle,
Select
FIRST_NAME,nvl(INCENTIVE_AMOUNT,0)
from
employee
right
join
incentives
on
A.EMPLOYEE_ID
B.EMPLOYEE_REF_ID
SQL Queries in MySQL, Select FIRST_NAME, IFNULL(INCENTIVE_AMOUNT,0) from employee a
right join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID
[12/20, 1:14 PM] Sagar thakare: 54. Select max incentive with respect to employee from
employee and incentives table using sub query SQL
Queries
in
Oracle,
nvl(max(INCENTIVE_AMOUNT),0) EMPLOYEE_ID)
SQL
Queries
from
INCENTIVES
Max_incentive
in
SQL
select
Server,
where from
select
DEPARTMENT,(select EMPLOYEE_REF_ID
EMPLOYEE
DEPARTMENT,(select
Max_incentive
from
EMPLOYEE
SQL
Queries
in
SQL
Server,
(max(INCENTIVE_AMOUNT),0) EMPLOYEE_ID)
from
select
DEPARTMENT,(select
INCENTIVES
where
Max_incentive
IFNULL
EMPLOYEE_REF_ID
from
EMPLOYEE
Advanced SQL Queries Interview Questions and Answers on "Top N Salary" - Examples 55.
Select TOP 2 salary from employee table SQL Queries in Oracle, select * from (select * from
employee order by SALARY
desc)
where
rownum
01-FEB-13 12.00.00.000000 AM
Banking
01-FEB-13 12.00.00.000000 AM
JOINING_DATE
---------------------------------------------------------------------------
DEPARTMENT
----------
Insurance
01-FEB-13 12.00.00.000000 AM
Insurance
JOINING_DATE
---------------------------------------------------------------------------
DEPARTMENT
----------
01-JAN-13 12.00.00.000000 AM
Services