Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
56 views

SQL Assignment

Uploaded by

Sagar Thakare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

SQL Assignment

Uploaded by

Sagar Thakare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

Sagar thakare: SQL*Plus: Release 11.2.0.2.

0 Production on Sun Dec 19 23:03:19 2021

Copyright (c) 1982, 2010, Oracle. All rights reserved.

SQL> conn sagar/sagar

Connected.

SQL> select * from employee;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------

JOINING_DATE

---------------------------------------------------------------------------

DEPARTMENT

----------

1 John Abraham 1000000

01-JAN-13 12.00.00.000000 AM

Banking

2 Michael Clark 800000

01-JAN-13 12.00.00.000000 AM

Insurance

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------

JOINING_DATE

---------------------------------------------------------------------------
DEPARTMENT

----------

3 Roy Thomas 700000

01-FEB-13 12.00.00.000000 AM

Banking

4 Tom Jose 600000

01-FEB-13 12.00.00.000000 AM

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------

JOINING_DATE

---------------------------------------------------------------------------

DEPARTMENT

----------

Insurance

5 Jerry Pinto 650000

01-FEB-13 12.00.00.000000 AM

Insurance

7 Testname1 123 650000

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------


JOINING_DATE

---------------------------------------------------------------------------

DEPARTMENT

----------

01-JAN-13 12.00.00.000000 AM

Services

8 Testname Lname% 600000

01-FEB-13 12.00.00.000000 AM

Insurance

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------

JOINING_DATE

---------------------------------------------------------------------------

DEPARTMENT

----------

6 Philip Mathew 750000

01-JAN-13 12.00.00.000000 AM

SERVICES

8 rows selected.

SQL> set linesize 500;


SQL> set pagesize 50;

SQL> select * from employee;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

8 rows selected.

SQL> select * from incentives;

no rows selected
SQL> create table Incentives2(employee_ref_id number(10), incentive_date date,
incentive_amount number(10));

create table Incentives2(employee_ref_id number(10), incentive_date date, incentive_amount


number(10))

ERROR at line 1:

ORA-00955: name is already used by an existing object

SQL> create table Incentives23(employee_ref_id number(10), incentive_date date,


incentive_amount number(10));

Table created.

SQL> insert into incentives values(1,'01-feb-13',5000);

1 row created.

SQL> insert into incentives values(2,'01-feb-13',3000);

1 row created.

SQL> insert into incentives values(3,'01-feb-13',4000);

1 row created.

SQL> insert into incentives values(1,'01-jan-13',4500);


1 row created.

SQL> insert into incentives values(2,'01-jan-13',3500);

1 row created.

SQL> desc incentives;

Name

Null? Type

-----------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------ --------
-----------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------

EMPLOYEE_REF_ID

NUMBER(10)

INCENTIVE_DATE

DATE

INCENTIVE_AMOUNT

NUMBER(10)

SQL> set linesize 500;

SQL> set pagesize 50;

SQL> select * from incentives;


EMPLOYEE_REF_ID INCENTIVE INCENTIVE_AMOUNT

--------------- --------- ----------------

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

SQL> select * from employee;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES
8 rows selected.

SQL> select first_name, last_name from employee;

FIRST_NAME LAST_NAME

-------------------- --------------------

John Abraham

Michael Clark

Roy Thomas

Tom Jose

Jerry Pinto

Testname1 123

Testname Lname%

Philip Mathew

8 rows selected.

SQL> select first_name employee_name from employee;

EMPLOYEE_NAME

--------------------

John

Michael

Roy

Tom
Jerry

Testname1

Testname

Philip

8 rows selected.

SQL> select upper(first_name) from employee;

UPPER(FIRST_NAME)

--------------------

JOHN

MICHAEL

ROY

TOM

JERRY

TESTNAME1

TESTNAME

PHILIP

8 rows selected.

SQL> select lower(first_name) from employee;

LOWER(FIRST_NAME)

--------------------
john

michael

roy

tom

jerry

testname1

testname

philip

8 rows selected.

SQL> select distinct department from employee;

DEPARTMENT

----------

Insurance

SERVICES

Banking

Services

SQL> select substr(first_name,0,3) from employee;

SUBSTR(FIRST

------------

Joh

Mic
Roy

Tom

Jer

Tes

Tes

Phi

8 rows selected.

SQL> select substring(first_name,0,3) from employee;

select substring(first_name,0,3) from employee

ERROR at line 1:

ORA-00904: "SUBSTRING": invalid identifier

SQL> select instr(first_name,'o') from employee where first_name = 'john';

no rows selected

SQL> select CHARINDEX('O'first_name,o) from employee where first_name = 'john';

select CHARINDEX('O'first_name,o) from employee where first_name = 'john'

ERROR at line 1:

ORA-00907: missing right parenthesis


SQL> select locate('O',first_name) from employee where first_name = 'john';

select locate('O',first_name) from employee where first_name = 'john'

ERROR at line 1:

ORA-00904: "LOCATE": invalid identifier

SQL> select RTRIM(FIRST_NAME) FROM EMPLOYEE;

RTRIM(FIRST_NAME)

--------------------

John

Michael

Roy

Tom

Jerry

Testname1

Testname

Philip

8 rows selected.

SQL> select LTRIM(FIRST_NAME) FROM EMPLOYEE;

LTRIM(FIRST_NAME)
--------------------

John

Michael

Roy

Tom

Jerry

Testname1

Testname

Philip

8 rows selected.

SQL> SELECT LENGTH(FIRST_NAME) FROM EMPLOYEE;

LENGTH(FIRST_NAME)

------------------

8 rows selected.
SQL> SELECT LEN(FIRST_NAME) FROM EMPLOYEE;

SELECT LEN(FIRST_NAME) FROM EMPLOYEE

ERROR at line 1:

ORA-00904: "LEN": invalid identifier

SQL> SELECT REPLACE(FIRST_NAME,'O','$') FROM EMPLOYEE;

REPLACE(FIRST_NAME,'

--------------------

John

Michael

Roy

Tom

Jerry

Testname1

Testname

Philip

8 rows selected.

SQL> SELECT * FROM EMPLOYEE;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE


DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

8 rows selected.

SQL> DESC TABLE EMPLOYEE;

Usage: DESCRIBE [schema.]object[@db_link]

SQL> SELECT FIRST_NAME||'_'||LAST_NAME FROM EMPLOYEE;

FIRST_NAME||'_'||LAST_NAME

-----------------------------------------

John_Abraham

Michael_Clark

Roy_Thomas
Tom_Jose

Jerry_Pinto

Testname1_123

Testname_Lname%

Philip_Mathew

8 rows selected.

SQL> SELECT FIRST_NAME +'_ '+LAST_NAME FROM EMPLOYEE;

SELECT FIRST_NAME +'_ '+LAST_NAME FROM EMPLOYEE

ERROR at line 1:

ORA-01722: invalid number

SQL> SELECT FIRST_NAME + '_' +LAST_NAME FROM EMPLOYEE;

SELECT FIRST_NAME + '_' +LAST_NAME FROM EMPLOYEE

ERROR at line 1:

ORA-01722: invalid number

SQL> SELECT FIRST_NAME, TO_CHAR(JOINING_DATE,'YYYY') JOINYEAR,


TO_CHAR(JOINING_DATE,'MON'),TO CHAR(JOINING_DATE,'DD') FROM EMPLOYEE;

SELECT FIRST_NAME, TO_CHAR(JOINING_DATE,'YYYY') JOINYEAR,


TO_CHAR(JOINING_DATE,'MON'),TO CHAR(JOINING_DATE,'DD') FROM EMPLOYEE

*
ERROR at line 1:

ORA-00936: missing expression

SQL> SELECT * FROM EMPLOYEE ORDER BY FIRST_NAME ASC;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

8 rows selected.

SQL> SELECT * EMPLOYEE ORDER BY FIRST_NAME DESC;

SELECT * EMPLOYEE ORDER BY FIRST_NAME DESC


*

ERROR at line 1:

ORA-00923: FROM keyword not found where expected

SQL> SELECT * FROM EMPLOYEE ORDER BY FIRST_NAME DESC;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

8 rows selected.

SQL> SELECT * FROM EMPLOYEE ORDER BY FIRST_NAME ASC, SALARY DESC;


EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

8 rows selected.

SQL> SELECT * FROM EMPLOYEE WHERE FIRST_NAME = 'JOHN';

no rows selected

SQL> Select * from Employee Where First_Name = 'John';

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE


DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

SQL> Select * from Employee Where First_Name in('John','roy');

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

SQL> Select * from Employee Where First_Name not in('John','roy');

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance
6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM
SERVICES

7 rows selected.

SQL> select * from EMPLOYEE where first_name like 'j%';

no rows selected

SQL> select * from EMPLOYEE where first_name like '%o%';

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

SQL> select * from employee where FIRST_NAME like'%n';

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking
SQL> select * from employee where FIRST_NAME LIKE('_N');

no rows selected

SQL> select * from employee where FIRST_NAME LIKE('_N')(UNDERSCORES);

select * from employee where FIRST_NAME LIKE('_N')(UNDERSCORES)

ERROR at line 1:

ORA-03001: unimplemented feature

SQL> SELECT * FROM EMPLOYEE WHERE FIRST_NAME LIKE 'j_'(underscores);

SELECT * FROM EMPLOYEE WHERE FIRST_NAME LIKE 'j_'(underscores)

ERROR at line 1:

ORA-00933: SQL command not properly ended

SQL> select * from Employee where salary > 600000;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking
2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM
Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

6 rows selected.

SQL> select * from Employee where salary < 800000;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES
6 rows selected.

SQL> select * from EMPLOYEE WHERE SALARY BETWEEN 500000 AND 800000;

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES

7 rows selected.

SQL> Select * from employee where FIRST_NAME in ('john','michael');

no rows selected

SQL> Select * from employee where FIRST_NAME in ('John','Michael');


EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

SQL> SELECT * FROM EMPLOYEE WHERE TO_CHAR(JOINING_DATE,'YYYY')='2013';

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE

DEPARTMENT

----------- -------------------- -------------------- ---------- --------------------------------------------------------------------------- ----------

1 John Abraham 1000000 01-JAN-13 12.00.00.000000 AM


Banking

2 Michael Clark 800000 01-JAN-13 12.00.00.000000 AM


Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00.000000 AM


Banking

4 Tom Jose 600000 01-FEB-13 12.00.00.000000 AM


Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00.000000 AM


Insurance

7 Testname1 123 650000 01-JAN-13 12.00.00.000000 AM


Services

8 Testname Lname% 600000 01-FEB-13 12.00.00.000000 AM


Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00.000000 AM


SERVICES
8 rows selected.

SQL> SELECT * FROM EMPLOYEE WHERE


SUBSTRING(CONVERT(VARCHAR,JOINING_DATE,103),7,4);

SELECT * FROM EMPLOYEE WHERE


SUBSTRING(CONVERT(VARCHAR,JOINING_DATE,103),7,4)

ERROR at line 1:

ORA-00936: missing expression

SQL> Select * from EMPLOYEE where SUBSTRING(convert(varchar,joining_date,103),7,4); =


'2013'

2 select * from EMPLOYEE where year(joining_date)='2013

3 select * from EMPLOYEE where year(joining_date)='2013';

ERROR:

ORA-01756: quoted string not properly terminated

SQL> select * from EMPLOYEE where year(joining_date)='2013'

SQL>

SQL> select * from employee where to_char(joining_date,'MM')='01' OR SELECT * FROM


EMPLOYEE WHERE TO_CHAR (JOINING_DATE,'MON') = 'JAN';

select * from employee where to_char(joining_date,'MM')='01' OR SELECT * FROM EMPLOYEE


WHERE TO_CHAR (JOINING_DATE,'MON') = 'JAN'

*
ERROR at line 1:

ORA-00936: missing expression

SQL> select * from EMPLOYEE where to_char(joining_date,'mm')='01' or select * from


EMPLOYEE where to_char (Joining_Date,'Mon') = 'Jan';

select * from EMPLOYEE where to_char(joining_date,'mm')='01' or select * from EMPLOYEE


where to_char (Joining_Date,'Mon') = 'Jan'

ERROR at line 1:

ORA-00936: missing expression

SQL> Select * from EMPLOYEE where SUBSTRING(convert(varchar,joining_date,100,1,3)='Jan';

Select * from EMPLOYEE where SUBSTRING(convert(varchar,joining_date,100,1,3)='Jan'

ERROR at line 1:

ORA-00936: missing expression

SQL>

[12/20, 1:00 PM] Sagar thakare: Table Name : Employee EMPLOYEE_ID FIRST_NAME
LAST_NAME SALARY JOINING_DATE 1

John

Abraham
DEPARTMENT

1000000 01-JAN-13 12.00.00 Banking AM

Michael

Clarke

800000

01-JAN-13 12.00.00 Insurance AM

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

01-JAN-13 12.00.00 Services AM

TestName1

123

650000

01-JAN-13 12.00.00 Services AM

TestName2

Lname%

600000
01-FEB-13 12.00.00 AM

Table Name : Incentives

EMPLOYEE_REF_ID INCENTIVE_DATE INCENTIVE_AMOUNT 1

01-FEB-13

5000

01-FEB-13

3000

01-FEB-13

4000

01-JAN-13
4500

01-JAN-13

3500

Insurance

[12/20, 1:01 PM] Sagar thakare: Insurance

SQL Queries Interview Questions and Answers on "SQL Select" - Examples 1. Get all employee
details from the employee table Select * from employee

2. Get First_Name,Last_Name from employee table Select first_name, Last_Name 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

[12/20, 1:01 PM] Sagar thakare: Equivalent

of

SQL Server

substr(FIRST_NAME,0,3)

SQL Server

Equivalent of Oracle

substring(FIRST_NAME,0,3)

SUBSTRING

is

SUBSTR,

from

SUBSTR is SUBSTRING, 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

8. Get position of 'o' in name 'John' from employee table

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

[12/20, 1:03 PM] Sagar thakare: Query:

Select

LOCATE('o',FIRST_NAME) from employee where first_name = 'John'


9. Get FIRST_NAME from employee table after removing white spaces from right side select
RTRIM(FIRST_NAME) from employee

10. Get FIRST_NAME from employee table after removing white spaces from left side select
LTRIM(FIRST_NAME) from employee

11. Get length of FIRST_NAME from employee table Oracle,MYSQL

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

len(FIRST_NAME) from employee

[12/20, 1:04 PM] Sagar thakare: :select

len(FIRST_NAME) from employee

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

concat(FIRST_NAME,'_',LAST_NAME) from EMPLOYEE

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),

DAY(joining_date) from EMPLOYEE

[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

Select * from employee order by FIRST_NAME asc,SALARY desc

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'

SQL Queries Interview Matching" - Examples

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'

[12/20, 1:07 PM] Sagar thakare: SQL

Queries

in

SQL

Server,
Select

from

SUBSTRING(convert(varchar,joining_date,103),7,4)

[12/20, 1:08 PM] Sagar thakare: EMPLOYEE =

where '2013'

SQL Queries in MySQL, Select * from EMPLOYEE where year(joining_date) = '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

[12/20, 1:09 PM] Sagar thakare: Server,

Select
*

from

EMPLOYEE

where

SUBSTRING(convert(varchar,joining_date,100),1,3) = 'Jan'

SQL Queries in MySQL, Select * from EMPLOYEE where month(joining_date) = '01'

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'

SQL Queries in Oracle, select to_char(JOINING_DATE,'dd/mm/yyyy hh:mi:ss') from

EMPLOYEE

SQL Queries in SQL Server, Select convert(varchar(19),joining_date,121) from

EMPLOYEE

SQL Queries in MySQL, Select CONVERT(DATE_FORMAT(joining_date,'%Y-%m-%d%H:


%i:00'),DATETIME)

from

EMPLOYEE

[12/20, 1:10 PM] Sagar thakare: EMPLOYEE


36. Get Joining Date,Time including milliseconds from employee table SQL

Queries

in

Oracle,

select

to_char(JOINING_DATE,'dd/mm/yyyy

HH:mi:ss.ff') from EMPLOYEE . Column Data Type should be “TimeStamp”

SQL Queries in SQL Server, select convert(varchar,joining_date,121) from EMPLOYEE

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

join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID

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

SQL Queries Interview Functions" - Examples

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

DEPARTMENT order by Total_Salary descending

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

employee group by DEPARTMENT order by Total_Salary descending

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')

Join_Year,to_char (JOINING_DATE,'MM') Join_Month,count(*) Total_Emp from employee

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

employee group by datepart(YYYY,JOINING_DATE), datepart(MM,JOINING_DATE)

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)

[12/20, 1:13 PM] Sagar thakare: month(JOINING_DATE)


48. Select department,total salary with respect to a department from employee table where
total salary greater than 800000 order by Total_Salary descending Select

DEPARTMENT,sum(SALARY)

Total_Salary

from

employee

group

by

DEPARTMENT having sum(SALARY) > 800000 order by Total_Salary desc

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 a left join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID

SQL Queries in SQL Server, Select FIRST_NAME, ISNULL(INCENTIVE_AMOUNT,0) from

employee

left

join
incentives

on

A.EMPLOYEE_ID

B.EMPLOYEE_REF_ID

SQL Queries in MySQL, Select FIRST_NAME, IFNULL(INCENTIVE_AMOUNT,0) from employee a


left join incentives B 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 a right join incentives B on A.EMPLOYEE_ID = B.EMPLOYEE_REF_ID

SQL Queries in SQL Server, Select FIRST_NAME, isnull(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

ISNULL(max(INCENTIVE_AMOUNT),0) from INCENTIVES where EMPLOYEE_REF_ID =


EMPLOYEE_ID)

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

[12/20, 2:34 PM] Sagar thakare: 3 Roy Thomas 700000

01-FEB-13 12.00.00.000000 AM

Banking

4 Tom Jose 600000

01-FEB-13 12.00.00.000000 AM

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------

JOINING_DATE

---------------------------------------------------------------------------
DEPARTMENT

----------

Insurance

[12/20, 2:36 PM] Sagar thakare: 5 Jerry Pinto 650000

01-FEB-13 12.00.00.000000 AM

Insurance

7 Testname1 123 650000

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY

----------- -------------------- -------------------- ----------

JOINING_DATE

---------------------------------------------------------------------------

DEPARTMENT

----------

01-JAN-13 12.00.00.000000 AM

Services

You might also like