You are on page 1of 46

SQL Interview Questions

Q1.What are the roles and responsibilties as Database tester?


What are the validations we are done for the data validation?
What are the schema validation?
1.Table structure validation/Metadata Testing –
▪ Validate table name as per srs document.
▪ Validate total columns as per srs.
▪ Validate each column name from table as per srs.
▪ Validate the sequence of column –
▪ Validate data type of each column –
▪ Validate data type size/length of each column.

2. Will check the impact of front end application on backend.

3. Constraint validation/Testing

4. Null Value validation

By using Joins, Aggreagte Functions, Rank and dense rank validated different type of data.

Q2. What is software testing?

valilation of software with respect to the customer requirement.

Q3. What is defect, data, database??

Data--- collection of meaningfull information.

Database-----collection of meaningfull information in computerised form.

Defect--------mismatch between actual result and the customer expected result.

Q4. What is difference between DBMS and RDBMS?

Sr. No DBMS RDBMS

1 Database management system Relational database management system

2 The data stores in file system Data stores in tabular form.

3 It is use for storing small volume of data. It is use for storing large valume of data.

4 There is no relation between two tables. There can be or can not be relation
between two or more table. As per the
customer requirements.

5 We can not use SQL in DBMS. We must use SQL in every RDBMS.

6 Eg. Excel, word, notepad, notepad++ Eg. Oracle, Oracle SQL developer, MySQL,
Microsoft SQL server.
Q5.what is SQL?

Sql is a structure query language. It is use for storing , manipulating and retrieving the data
from database.

Q6.what are SQL statements?

There are 4 sql statements

1. ddl(data definition language)


2. dml(data manipulation language)
3. dcl(data control language)
4. tcl(transactional control language)

1.ddl-----alter,truncate,drop,rename,create

This commands are related to the table structure.

Alter is used for changing the column name, column datatype, column size.it is also use for
adding the column and deleting the column from existing table.

Truncate is use for deleting the table data and keeping table structure as it is.

Drop is use for deleting whole table data and table structure of existing table.

Rename is use for renaming the table name and column name

Create is use for creating the table.

2.dml------update, delete, select, insert

This commands are related to the table data

Update is use for updating the particular row of the table and also updating particular
column data.

Delete is use for deleting particular row of the table.

Select is use for displaying the table data.

Insert is use for inserting the data into the table.

3.dcl------grant,revoke

Grant is use for giving acces permission to the databse tester.

Revoke is use for ungrant the permission.

4.tcl------commit,rollback

Commit is use for saving transactions permanently.

Rollback is use for rollback the transaction.


Q7.Difference between Delete,Drop and Truncate commands

Sr. No Drop Truncate Delete

1 It is DDL statement It is DDL statement It is DML statement


command command command

2 It is used to delete table It is use used to delete table It is used to delete all the
structure and table data data and keeping table data and delete row
structure as it is according to specific
condition

3 Roll back is not possible Roll back is not possible Roll back is possible

4 We can not apply where We can not apply where We use where clause
clause with drop command clause with drop command
Delete from tablename;
Drop table tablename; Truncate table tablename;
Delete from tablename
where eid=101;

Q8.Difference between Alter and Update Command

Sr.No Alter Command Update Command

1 It is DDL statement command It is DML statement command

2 It is related to the table structure It is related to table data

3 It is used to change data type, name, size It is used to update existing data within the
of column. It is also used to add and table.
delete column from existing table

Q…..difference between DDL and DML commands

Seq.no. DDL DML

1. Ddl stands for data definition DML stands for data manipulation
language language

2. DDL commands are related to the DML commands are related to the
table structure. table data.

3. Create, drop, truncate, rename, alter Select, update, delete, insert are the
are the ddl commands dml commands

4. Rollback is not possible Rollback is possible


Q9.how to display table information?

How to display column name and their information?

By using describe command.

Describe tablename;

Q10.difference between varchar and varchar2?

Seq. no. varchar Varchar2

1 2000 bytes 4000 bytes

Varchar(20)---5 char wasted Varchar(20)---5 char realeased

It will improve system


performance
It will decrese system performance

Q11.how to display null value from particular column?

By using is null keyword. Is null keyword is only use with where clause.

Select * from emp where eid is null;

Select * from emp where eid is not null;…..is not null keyword

Q12.update the salary of all employee from the table?

Update tablename set esal=120000;

Q13.update the employee salary with 10%?

update ksh10 set ksal= ksal*1.1;

update ksh10 set ksal=ksal+(ksal*10/100);

Q14.how to update salary with 12000 from table whose ename is ‘kshitija’?

Update emp set esal=12000 where ename=’kshitija’;

Q15.how to update odd and even record?

update ksh10 set ksal=75000 where mod(id,2)=0;......updating even records salary with 75000

update ksh10 set ksal=75000 where mod(id,2)=1;......updating odd recors salary with 75000

Q16.how to display odd and even records?

select * from ksh10 where mod(id,2)=0;……..even records

select * from ksh10 where mod(id,2)=1;……..odd records


Q17.how to display positive(+) and negative(-) value from particular column?

Select (case when cbill >0 then cbill else 0 end) as positive_value from emp;

Select (case when cbill <0 then cbill else 0 end) as negative_value from emp;

Q18.how to display sum of positive(+) and sum negative(-) value from particular column?

Select sum(case when cbill >0 then cbill else 0 end) as sum_of_positive_value from emp;

Select sum(case when cbill <0 then cbill else 0 end) as sum_of_negative_value from emp;

Q19.how to display gender M or F from particular table?

select (case when gender='m' then 'male' else 'female' end) as genderfind from qqqqqqqqqq;

Q20.how to copy only table structure to the another table?

create table puscopy as select * from ksh10 where 10=12;

Q21.how to copy one table to the another table?

create table pus111 as select * from ksh10;

Q22.how to delete null value from table?

Delete from emp where eid is null;

Q23.how to delete particular column value from table?

Update emp set esal=’’ where eid =101;

Q24.which options are use to delete null value from table?

1…Delete * from emp where eid is null;

2…Delete from emp where eid is null; ………. Correct option

3…Delete from emp where eid =’ ‘;

4…Delete from emp where eid =(null);

Q25.differen between between and in operator?

Seq.no between In

1. Display the possible range of Display particular set of


values from column values from column

2. It applies on numbers and dates It applies on number, dates


value and character values

3. Select * from tablename where Select * from tablename


esal between 10000 and 50000; where esal in(100,200,300);
Q26.how to concate first name and lastname from the table?

By using concatenation operator( || )

Select (first_name || last_ name ) as ful_name from emp;

Select emp.*, (first_name || ‘_’ || last_name) as full_name from emp;

Q27.show the records who is leave in ‘newyork’ having salary 200000 and name id ‘kshitija’?

Select * from emp where eadd =’newyork’ and esal=200000 and ename=’kshitija’;

Q28. All questions on like operator?

%------unknown length of the string

_ ------unknown single character

select * from logic where firstname like 'p%';...............................start with p

select * from logic where firstname like '%a';......................................end with a

select * from logic where firstname like '%a_';....................................second last character is a

select * from logic where firstname like '_a%';.....................................second character is a

select * from logic where firstname like '%a_ _';.......................................third last charcater is a

select * from logic where firstname like ‘_ _ _ _ _';...............................5 character string

select * from logic where firstname like '%';.............................unknown length of the string

select * from logic where firstname like '%a%';......................string contain a

select * from logic where firstname like '%l%' and firstname like '%k%';.......string contain both l
…………………………………………………………………………………………………………………….and k character

select * from logic where firstname like '%l%' or firstname like '%k%';.........................string contain
………………………………………………………………………………………………………………………either l or k character

select * from logic where firstname like 'k%' and firstname like '%l';.........................string start with k
…………………………………………………………………………………………………………………………….and end with l

select * from logic where firstname like 'k%' or firstname like '%l';..........................string start with k
………………………………………………………………………………………………………………………………or end with l
Q28. Difference between primary key and unique constraints?

Sr.No Primary Key constraints Unique constraints

1 It does not accept duplicate and null It does not accept duplicate value but accept
values in respective column null values

2 Any table has only one primary key Any table has more than one unique constraints
constraints

3 It accepts unique and not null values It accepts any number of null values

4 Create table emp(eid number(5) Create table emp (eid number(5),cname


primary key, ename varchar2(20)); varchar(20) unique, cadd varchar2(20) unique);

Here eid column does not accepts Here cname and cadd column will not accepts
duplicate and null values. duplicate value but accepts null values.

Null value can be duplicated cause its an


unknown value

Q29. Difference between primary key and foreign key constraints

Sr. No Primary Key Foreign Key

1 It does not accept duplicate and null It accepts duplicate and null value
value

2 It does not maintain relation It maintain the relation between two tables .
between two tables
Primary key of one table is foreign key of
another table

3. create table Cust_Invoice21 (oid create table Cust_Invoice21 (oid


number(6) primary key,oname number(6),oname varchar2(15));.....parent table
varchar2(15));
create table Cust_Invoice21 (oid
number(6),oname varchar2(15),cid number(7)
references cust_order(cid));......child table

4. We can have only one primary key in We can have more than one foreign key in a
a table table
Q30. Difference between primary key and composite key constraints

Sr. No Primary Key composite Key

1 It is applied on only one column in a It is a primary key applied on more than one
table column in a table

2 create table Cust_Invoice21 (oid create table Cust_Invoice21 (oid


number(6) primary key,oname number(6),oname varchar2(15),eadd
varchar2(15)); varchar(20),esal number(10), primary
key(oid,oname,esal));

here primary key will applied on column


here oid column will not accepts
oid,oname,esal. if all of from this 3 column value
duplicate and null value
are same then error will show and one of them
column value is different then it executed.

One of them from this 3 column cannot accepts


null value

Q31. Do you know aggregate functions?

Following are the aggregate functions-----max, min, sum, avg, count

select min(esal) from aggre;

select max(esal) from aggre;

select sum(esal) from aggre;

select avg(esal) from aggre;

select count(esal) from aggre;....can not count null value

select count(*) from aggre;……this will count the no of rows of the table..it contain null also

Q32. Do you know character functions?

Yes , there are two types of character function:

1. case manipulation function( scalar function)


• lower
• upper
• initcap
2. character manipulation function
• substr
• instr
• length
1.Scalar functions(case manipulation function):

select lower(ename) from aaaa;.........................all letter in lowercase

select upper(ename) from aaaa;.......................all letter in uppercase

select initcap(ename) from aaaa;...........intital letter capital and remainning in lowercase

select lower('kshitija') from aaaa;.........all letter of kshitija in lowercase

select upper('kshitija') from aaaa;..........all letter of kshitija in uppercase

select initcap('kshitija') from aaaa;..........intital letter capital and remainning in lowercase

select aaaa.*,lower(edept) as lowercasedept,upper(edept) as uppercasedept,initcap(edept)


as initialletterdept from aaaa;

2.character manipulation functions:

length----calculate the length of string.

select length('kshitija') from dual;----8

select length('kshitija') from aaa;-----8

select length(ename) as lenght_of_names from aaa;

select length(esal) from aaa;

select aaa.*,length(edept) as lenght_ofeach_dept from aaa;

select length(‘kunal’) as lenght_of_names from dual;…….5

substr……finding the substring

substr(columnname,x,y)

x=starting position of character where substring starts.

y=length of substring from that starting character x

lenght of the string cannot be a negative value

select substr('kshitija',1,4) from dual;.......kshi

select substr('kshitija',2,4) from dual;........shit

select substr('kshitija',-1,4) from dual;......a


select substr('kshitija',-3,3) from dual;-----ija

select substr('kshitija',-3,-3) from dual;------(null)

select substr('kshitija',4,-4) from dual;-----(null)

select substr(cname,1,4) from ksh10;.......finding substring from each name of the column cname;

select aaa.*,substr(ename,1,4) as name_substring ,substr(edept,1,3) as depatment_sunstring from


aaa;

select aaa.*,substr(ename,-4,3) as name_substring,substr(edept,-3,3) as department_substring from


aaa;

instr-----this function will return the location of substring from the string

instr(columnname, substring, x, y);

select instr('sangharsha','h') from dual;......this query will return the location of first 'h' of string.....5

select instr('sangharsha','harsh') from dual;......5......while finding the location of 'harsh' substring it


will take the location of 1st letter of 'harsh'

select instr('sangharsha','g') from dual;....................4

select instr('sangharsha','h',1,1) from dual;.......5.....location of 1st 'h'

select instr('sangharsha','h',1,2) from dual;.........9 .....location of 2nd 'h'

select instr('sangharsha','a',1,1) from dual;........2......location of 1st 'a'

select instr('sangharsha','a',1,3) from dual;........10......location of 3rd 'a'

select instr('sangharshaghargharghar','ghar',1,1) from dual;.....4

select instr('sangharshaghargharghar','ghar',1,3) from dual;.....15

select instr('sangharshaghargharghar','ghar',1,2) from dual;.....11

select instr('sangharshaghargharghar','ghar',1,4) from dual;.....19

select instr('sangharsha','h',2,1) from dual;..........5.....

select instr('sangharsha','h',4,1) from dual;..........5

select instr('sangharsha','h',6,1) from dual;..........9

select instr('sangharshha','h',6,2) from dual;..........10..........it will take the subtring after the starting
position what we have given but counting from 1st letter of the string
select instr('kshitija','h',1) from dual;.....3

select instr('kshitija','i',1,2) from dual;....6

select instr('kshitija','i',1) from dual;......4

select instr('kshitija','i',2) from dual;......4

select instr('kshitija','i',2,2) from dual;....6

select instr('kshitija','i',-4) from dual;...4

select instr('kshitija','i',-4,2) from dual;....0......when we will give starting position in minus eg. -4
then it will take position of substring from right to left but counting from left to right

select instr('kshitija','i',-4,1) from dual;....4

Q33. Finding the given output from given data

table name: product_info2

column name: pname

pname values:TV123456789LG101

TV198765435Samsung199

TV198765435Samsung201

TV123456789Onida878

Output:

productno_modelno

123456789_101

198765435_199

198765435_201

123456789_878

Answer:

select substr(pname,3,9) from product_info2;

select substr(pname,-3,3) from product_info2;

select (substr(pname,3,9) || '_' || substr(pname,-3,3)) as productno_modelno from


product_info2;
Q34. Finding the given output from given data

input:

column name:email

column values: kshitijaweljali@gmail.com

yusuf.tamboli@outlook.com

tushark@credence.in

pushpak.waje@gmail.com

output:

user_name domain_name

kshitijaweljali gmail.com

yusuf.tamboli outlook.com

tushark credence.in

pushpak.waje gmail.com

answer:

select substr(email , 1 , (instr(email, '@' )-1) ) from empp;

select substr(email , ( instr(email , '@') +1) ) from empp;

select (substr(email , 1 , (instr(email, '@' )-1) )) as username , (substr(email , (


instr(email , '@') +1) ) ) as domainname from empp;

Q35..do you know date functions??

add_months......

select add_months(doj,6) from datee;......adding 6 months to the doj column

select add_months(dor,6) from datee;......adding 6 months to the dor column

select add_months('01-01-2022', 5) from dual;........adding 5 months in 1-01-2022

select add_months('1-03-2005',5) from dual;........adding 5 months in 1-03-2005

months_between-----

select months_between(doj,dor) from datee;...........it will give negative value i.e. months
between doj and dor is in negative

select months_between(dor,doj) from datee;............display months between dor and doj


select months_between('01-06-1997','02-03-1997') from dual;

select months_between(dor,doj) from datee where eid=101;............display experience in


months of eid=101

select (months_between(dor,doj)/12) from datee; ................display experience in year

select (months_between(dor,doj)/12) from datee where eid=102;........display experience in


year whoes eid=102

next_day..........

select next_day('31-03-2022','thu') from dual;..........display next thurseday

select next_day('07-04-2022','thu') from dual;.........display next thurseday....14-04-2022

select next_day('11-4-2022','wed') from dual;..........13-04-22.....wed

select next_day(doj,'mon') from datee;.......display next monday of each doj from table datee

last_day.........

select last_day(doj) from datee;.........display last day of the perticular month

select last_day('30-mar-2022') from dual;.....31-03-2022

select last_day('19-03-2022') from dual;......31-03-2022

select last_day('11-04-2022') from dual;.....30-4-2022

sysdate........

select sysdate from dual;.....display system date

systimestamp.......

select systimestamp from dual;........31-03-22 9:32:37.672000000 AM +05:30

to_date………

Q36.finding age of you??

select months_between(sysdate,'06-08-1997') from dual;......display my age in months

select (months_between(sysdate,'06-08-1997') / 12) from dual;.....display my age in years


Q37.which datatype is use to enters dates into the table??

Date is a datatype which is used to enter dates into the table

create table datee(eid number(10),ename varchar(20),doj date,dor date);

insert into datee values(101,'kshitija','1-01-2019','1-02,2020');

Q38.what is to_date function??

to_date.....it will convert character or string value to date value

insert into datt values(001,'mayur','2012-12-01');........it will display error 'not a valid date' cause
format of date is wrong ....to overcome this issue we use to_date function

insert into datt values(002,'kunal',to_date('2012-12-01','yyyy-mm-dd'));

insert into datt values(003,'yusuf',to_date('12-01-2012','mm-dd-yyyy'));

insert into datt values(101,'kshitija',to_date('jan-22-2022','mm-dd-yyyy'));

insert into datt values(102,'kunal',to_date('07-12-03','mm-dd-yy'));

insert into datt values(103,'pragati',to_date('22-08-2001','dd-mm-yyyy'));

insert into datt values(104,'riya',to_date('2007-12-03','yyyy-dd-mm'));

insert into datt values(105,'amit',to_date('22-2002-10','dd-yyyy-mm'));

insert into datt values(001,'mayur','02-12-2012');........formate(dd-mm-yyyy) of date when we use


date datatype

Q39.what is to_char function??

to_char....it will convert date/numeric value to character/string value

select * from datee;

select to_char(doj,'yyyy') from mkl;

select to_char(doj,'dd') from mkl;

select to_char(doj,'mm') from mkl;

select to_char(doj,'dd-mm') from mkl;

select to_char(doj,'mon-dd') from mkl;.......display jan,jun,feb when we use mon

select to_char(doj,'month-day') from mkl;......display january-Tuesday

Q40. can you display employees joinning year along with ename??

select ename,to_char(doj,'yyyy') from mkl;


Q41.displaying current year of system

select to_char(sysdate,'yyyy') from dual;

Q42.can you display employee data who join in particular month/year??..2019

select * from mkl where to_char(doj,'yyyy')=2019;.displaying records whoes joining year is 2019

select * from mkl where to_char(doj,'mm')=01;........displaying records whoes joining month is 01

select * from mkl where to_char(doj,'month')='january';

select * from mkl where to_char(doj,'mm-yyyy')='01-2019';

Q43.displaying the employee data who join in particular quarter??

select * from mkl where to_char(doj,'q')=1;.......displaying employee data in first quarter i.e. in
jan,feb,march month

select * from mkl where to_char(doj,'q')=2;.......displaying employee data in 2nd quarter i.e. in
april,may,june month

select * from mkl where to_char(doj,'q')=3;.......displaying employee data in 3rd quarter i.e. in
july,aug,sep month

select * from mkl where to_char(doj,'q')=4;.......displaying employee data in 4th quarter i.e. in
oct,nov,dec month

Q44. displaying the employee data who join in particular quarter and year?? 1st quarter and year
2019

Select * from mkl where to_char(doj,’q’)=1 and to_char(doj,’yyyy’)=2019;

Q45.displaying employee data who join in particular date 01-01-2016??

select * from mkl where to_char(doj,'dd-mm-yyyy')='01-01-2016';

Q46.displaying employee data who join on between given dates ‘05-25-2017’ and ‘jan-01-2019’
using date_function??

select * from mkl where doj between to_date('05-25-2017','mm-dd-yyyy') and to_date('jan-


01-2019','mon-dd-yyyy');

Q47.displaying employee data who join on between given dates ‘05-25-2017’ and ‘jan-01-2019’
and ‘january-26-2018’using date_function??
select * from mkl where doj in( to_date('05-25-2017','mm-dd-yyyy') , to_date('jan-01-
2019','mon-dd-yyyy'), to_date('january-26-2018','month-dd-yyyy'));

Q48.how to display unique records from particular column??

By using dinstinct keyword

Select dintinct(edept ) from emp;------display unique depet

Select count(distinct(edept)) from emp;-----count of unique dept

Q49.display the employee information who join on partucular date(apr-30-2012) using date
function??

select * from dattt where doj=to_date('april-30-2012','month-dd-yyyy');

Q50.what is NVL??

NVL converts null value to the actual value in select statements;

Select nvl(eid,101) from emp;

Select nvl(ename,’kshitija’) from emp;

Select emp.*, nvl(ename,’kshitija’) as nvlname ,nvl(eid,102) as nvlid from emp;

Q51.what is decode??

Decode will convert small abbreviations to the full name.

Select decode(mmm,'jan','january','feb','february','mar','march','apr','april','may','may',

'jun','june','jul','july','aug','august','sep','september','oct','october','nov','november','dec',

'december','','unknown') from monthss;

mmm-----is column name

select dfg.*,decode(ename,'kshitija','kshitija weljali','','unknown') from dfg;

Q52.types of of sql clauses

1..where clause

2..group by clause

3..having clause

4..order by clause
Q53.where clause

It filters the rows from table according to the specific conditions and display/delete/update the
records.

Select * from emp where edept=’cse’;

Select * from emp where edi=’101’;

Select * from emp where eid is null;

Select * from emp where eid is not null;

Q54.group by clause

It divides multiple rows into the identical groups.

We can use only group by column and aggregate function column(with any column name fom
the column) in select statement.

Or both group by column and aggregate function column(with any columnname from the table )

In select statement.

select edept from tyuu group by edept;...................display unique department

select count(edept) from tyuu group by edept;.............display count of each unique


department....here value of (null) department is 0

select count(*) from tyuu group by edept;..............display count of each unique


department....here value of (null) department is 4

select edept ,count(edept) from tyuu group by edept;

select count(eadd) from tyuu group by edept;

select edept from tyuu group by edept;..............display unique department from column

select edept,count(edept)from tyuu group by edept;............display unique department and their


count...here (null) dept value is 0 because count(dept) will count rodept not the row

select edept,count(edept) from tyuu group by edept where count(edept) >1;........we cannot use
where clause after group by

select edept,count(*) from tyuu group by edept;.............here (null) dept value is 4 because count(*)
will count row not the department

select edept,count(eid) from tyuu group by edept;


select edept,count(eadd) from tyuu group by edept;

select edept,count(emob) from tyuu group by edept;

select edept,count(ename) from tyuu group by edept;

select edept,count(*) from tyuu group by edept

select ename from tyuu group by edept;---- It will not run the query...We can use only group by
column or aggregate fun column(with any column from table) in select statement.Or both group by
column and aggregate fun column(with any column from table).

select edept,eid from tyuu group by edept---It will not run the query

select ename,edept from tyuu group by edept-----It will not run the quer

How to apply group by clause on 2 columns

select ename,edept,count(*) from tyuu group by edept,ename;

select esal,edept,count(*) from tyuu group by edept,esal;

select ename,edept,esal,count(*) from tyuu group by edept,ename,esal;

Q55..display departmentwise count of employee

select edept,count(*) from tyuu group by edept; ......here (null)=4

select count(edept) from tyuu group by edept;.............here value of (null) department is 0

Q56.display departmentwise heighest salary

select max(esal) from tyuu group by edept;

select edept,max(esal) from tyuu group by edept;

Q57.display department wise min salary

select edept,min(esal)from tyuu group by edept

Q58..display department wise total salary

select edept, sum(esal) from tyuu group by edept;

Q59..display department wise average salary

select edept , avg(esal) from tyuu group by edept;


Q60..display departmentwise minimum and average salary

select edept,min(esal),avg(esal) from tyuu group by edept;

Q61.how to display productwise heighest sales

pid pname sales_amt

101 lap 80000

102 mob 60000

103 lap 50000

104 mob 8000

105 pd 125000

106 mob 6000

107 pd 40000

select * from proo;

select pname,max(sales_amt) from proo group by pname;

Q62.having clause

It applies filter on results which are generated by group by clause using specific aggregate
function.

Q63.how to display duplicate records

select edept, count(*) from tyuu group by edept having count(*)>1;

organisation working query:

select eid,ename,eadd,edept, count(*) from tyuu group by edept,eid,ename,eadd having


count(*)>1;

Q64.how to display unique records

select edept, count(*) from tyuu group by edept having count(*) = 1;

organisation working query:

select eid,ename,eadd,edept, count(*) from tyuu group by edept,eid,ename,eadd having


count(*)=1;
Q65.order by clause

it will sort the records in ascending and dscending order

by default sequence of order by clause is ascending

asc....0 to 9, a to z, A to Z

special character...number....capital letter ...small letter

desc...9 to 0,z to a, Z to A

small letter…….capital letter……numbers……special character

select * from tyuu;

select * from tyuu order by esal desc;

select * from tyuu order by esal asc;

select esal from tyuu order by esal desc;

select ename from tyuu order by ename;

select esal from tyuu order by esal desc nulls last;

select esal from tyuu order by esal asc nulls first;

select max(ename) from tyuu;……………..select maximum ename

select min(ename) from tyuu;……………….select min ename

select edept ,count(*) from tyuu

where eid >104

group by edept

having count(*)>1

order by edept desc nulls last;

select edept,count(*) as cnt from tyuu

where eid > 104

group by edept

having count(*) >1

order by cnt desc nulls last;


select edept,count(*) from tyuu

where eid > 104

group by edept

having count(*) >1

order by count(*) desc nulls last;

Q66..what id difference between where and having clause

Sr.No Where Clause Having Clause


1 It applies filter on table rows based It applies filter on the result generated by group
on specific condition by clause using specific agreegate function

2 We use where clause before groupby We use having clause after groupby

3 We can not use aggregate function in We must have to use aggregate function in hving
where clause for applying specific clause to apply filter on the result generated by
condition groupby clause

4 We can use where clause with or We can not use having clause without groupby
without groupby

Q67. Internal execution order of any query by database engine

1. From
2. Where
3. Group by
4. Having
5. Select
6. Order by

Q68. Drop command

It is ddl statement command.

It will delete table structure and table data.

Rollback is not possible.

We can not apply where clause with drop command.

Drop table tablename;


Q69. Truncate command

It is ddl statement command.

It will delete table data and kepping table structure as it is.

Rollback is not possible.

We can not apply where clause with truncate command.

truncate table tablename;

Q70. Rename command

It is ddl statement command.

We can change table or column name using this command.

Rename aaa to bbb;

aaa……already table name

bbb……renamimng table name

Q71. Alter command

It is ddl statement command.

It deals with table structure .

Rollback is not possible.

This command is use to change column name,datatype, size and adding and deleting the
column from existing table.

Q72.how to add 1 new column to the existing table?

alter table tyuu add cccc number(5);

Q73.how to add multiple new column to the existing table?

alter table tyuu add (cccc number(5), bbbb varchar(20));

Q74.how to drop column to the existing table?

alter table tyuu drop column cccc ;

Q75.how to add multiple new column to the existing table?

alter table tyuu drop (ccc , bbbb );


Q76.how to rename a column?

alter table tyuu rename column aaaa to bbbb;

Q77.how to modify datatype / size of column?

alter table tyuu modify(emob number(25));

alter table tyuu modify(empid varchar2(25));

we can change the datatype or size of the column only if the column contain null values.

Q78.what is subquery?

Subquery is query within query. There are two parts in subquery 1. Inner query 2. Outer query

Execution process…first it will execute inner query and then execute outer query

And while executing outer query ,output of inner query will be used as input for outer query

1.....find out the employee details with heighest salary

select * from aggre;

select * from aggre where esal=(select max(esal)from aggre);

2....find out the employee details with lowest salary

select * from aggre where esal= (select min (esal) from aggre);

3.... how to display second max salary??...............very important quetion

select max(esal) from aggre where esal<(select max(esal) from aggre);

4.....how to display second min salary???.....vvimp que

select min(esal) from aggre where esal>(select min(esal) from aggre);

5.....how to display second min salary with all details???.....vvimp que

select * from aggre where esal=

(select min(esal) from aggre where esal>

(select min(esal) from aggre));


6.....how to display second max salary with all details???.....vvimp que

select * from aggre where esal=

(select max(esal)from aggre where esal<

(select max(esal) from aggre));

7.....how to display 3rd min salary with all details???.....vvimp que

select * from aggre where esal=

(select min(esal) from aggre where esal>

(select min(esal) from aggre where esal>

(select min(esal) from aggre)));

8.....how to display 5rd max salary with all details???.....vvimp que

select * from aggre where esal=

(select max(esal) from aggre where esal<

(select max(esal) from aggre where esal<

(select max(esal) from aggre)));

9.....how to display 4th min salary with all details???.....vvimp que

select * from aggre where esal=

(select min(esal) from aggre where esal>

(select min(esal) from aggre where esal>

(select min(esal) from aggre where esal>

(select min(esal) from aggre))));

10.....how to display 4th max salary with all details???.....vvimp que

select * from aggre where esal=

(select max(esal) from aggre where esal<

(select max(esal) from aggre where esal<

(select max(esal) from aggre where esal<

(select max(esal) from aggre))));

Q79..pseudo columns

This are the virtual columns from database

1.rank()

2.dense_rank()

3.rownum 4.rowid
Q80. Difference between rank() and dense_rank()

Seq . Rank() Dense_rank()


no.

1. It applies ranking mainly on number It also applies ranking mainly on number


values for ex. Salary and gives ranking values for ex. Salary and gives ranking
according to sorting order of asc and according to sorting order of asc and desc
desc ranking. ranking.

2. If there are two duplicate values then it It if there are two duplicate values then it
will give same ranking for both values will give same ranking for both values
and skip the next ranking and gives the and gives next ranking to the next values
ranking same as rownumber to the next ,do not skip next ranking like rank()
value

3 select esal,rank() over (order by esal select esal, dense_rank() over(order by


desc nulls last) as ranking from tyuu; esal desc nulls last) as ranking from tyuu;

Q81....finding heighest salary of employee with details

select * from (select tyuu.*, dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking =1;

Q82....finding lowest salary of employee with details

select *from (select tyuu.*, dense_rank() over(order by esal asc nulls last ) as ranking from
tyuu)where ranking =1;

Q83.....finding 2nd heighest salary of employee with details

select * from (select tyuu.*,dense_rank() over(order by esal desc nulls last) as ranking from
tyuu) where ranking=2;
Q84.....finding 2nd minimum salary of employee with details

select * from (select tyuu.*, dense_rank() over(order by esal asc nulls last) as ranking from tyuu)
where ranking=2;

Q85......finding 3rd max salary of employee with details

select * from (select tyuu.*, dense_rank() over(order by esal desc nulls last) as ranking from
tyuu) where ranking=3;

Q86......finding 4th max salary of employee with details

select * from (select tyuu.*, dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking=4;

Q87..... finding 64th max salary of employee with details

select * from (select tyuu.*, dense_rank() over(order by esal desc nulls last) as ranking from tyuu)
where ranking =64;

Q88.....finding 1000th max salary of employee with details

select * from (select tyuu.*,dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking=1000;

Q89.....finding top 5 heighest salary of employee with details

select * from (select tyuu.*,dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking <=5;

Q90.....salaries between 3rd max to 6th max

select * from (select tyuu.*,dense_rank() over(order by esal desc nulls last) as ranking from tyuu)
where ranking between 3 and 6;

Q91.....particular salaries 3rd max, 5th max , 8th max

select * from (select tyuu.*,dense_rank() over (order by esal desc nulls last) as ranking from tyuu)
where ranking in(3,5,8);
Q92.....particular salaries which are not 3rd max, 5th max , 8th max

select * from (select tyuu.*,dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking not in(3,5,8);

Q93.....finding 4th min salary of employee with details

select * from (select tyuu.*,dense_rank() over(order by esal asc nulls last) as ranking from tyuu)
where ranking =4;

Q94…rownum

It will generate temporary sequence number.

select tyuu.*,rownum from tyuu;

Q95…select 1st five records from table

select tyuu.*,rownum from tyuu where rownum <=5 order by rownum asc nulls last ;

Q96…select last five records from table

select * from (select tyuu.*,rownum as seqno from tyuu order by seqno desc) where rownum
<=5 order by seqno ;

Q97…rowid

It is unique id of each row of the table. It is permanent.

select tyuu.*,rowid from tyuu;

Q98…select last inserted row from table

select * from tyuu where rowid=(select max(rowid) from tyuu);

Q99…select 1st inserted row from table

select * from tyuu where rowid=(select min(rowid) from tyuu);

Q101…select departmentwise latest rowid

select edept,max(rowid) from tyuu group by edept;


Q102…select max rowid of cse dept

select edept,max(rowid) from tyuu where edept =’cse’ group by edept;

Q103…select latest row of cse dept

select * from tyuu where rowid=(select max(rowid) from tyuu where edept='cse');

sql joins....it display the data from miltiple table

Q104.....do we need forein key to join the two table??

Ans: no

inner join....It dispalys the matching records(common data) from both side table based on specific
condition.

create table cust_order11(cid number(5),cname varchar(20),ccity varchar(20));

select * from cust_order11;....1st table

create table cust_invoice22(oid number(5),oname varchar(20),billamt number(20),custid


number(5));

select * from cust_invoice22;....2nd table

select * from cust_order11 inner join cust_invoice22 on cust_order11.cid=cust_invoice22.custid;

on.....Its a clause which is use to apply specific condition during joining the tables.

select * from cust_order11 c1 inner join cust_invoice22 c2 on c1.cid = c2.custid;.........................here


c1 and c2 are object

select * from cust_order11 c1 inner join cust_invoice22 c2 on c1.cid = c2.custid where c1.cid
<=102;.....display common data from both table having cid <=102

select c1.cname,c1.ccity,c2.oname,c2.billamt from cust_order11 c1 inner join cust_invoice22 c2 on


c1.cid=c2.custid;

select * from cust_invoice22 c1 inner join cust_order11 c2 on c1.custid=c2.cid;


outer join .......

left outer join(left join) ....it displays commom data from both the table and remaining data from
left side table. for tthat remaining data from left side table it displays null to right side table.

create table cust_order11(cid number(5),cname varchar(20),ccity varchar(20));

select * from cust_order11;....1st table

create table cust_invoice22(oid number(5),oname varchar(20),billamt number(20),custid


number(5));

select * from cust_invoice22;....2nd table

select * from cust_order11 c1 left outer join cust_invoice22 c2 on c1.cid=c2.custid;

select * from cust_order11 c1 left outer join cust_invoice22 c2 on c1.cid=c2.custid where cid >102;

select c1.cname,c1.ccity,c2.oname,c2.billamt from cust_order11 c1 left outer join cust_invoice22 c2


on c1.cid=c2.custid;

select c1.cname,c1.ccity,c2.oname,c2.billamt from cust_order11 c1 left outer join cust_invoice22 c2


on c1.cid = c2.custid where cid >102;
right outer join(right join).....it displays the common data from two table and also displays the
remaining data from right table. for that remaining data from right side table it displays null to left
side table.

select *from cust_order11 c1 right outer join cust_invoice22 c2 on c1.cid = c2.custid ;

select * from cust_order11 c1 right outer join cust_invoice22 c2 on c1.cid = c2.custid where cid
>102;...

select * from cust_order11 c1 right outer join cust_invoice22 c2 on c1.cid = c2.custid where custid
>102;...

select c1.cname,c1.ccity,c2.oname,c2.billamt from cust_order11 c1 right join cust_invoice22 c2 on


c1.cid=c2.custid;

full outer join(full join)....it displays the commom data from both table. it is combination of left
outer join and right outer join.

select *from cust_order11 c1 full outer join cust_invoice22 c2 on c1.cid = c2.custid ;


select * from cust_order11 c1 full outer join cust_invoice22 c2 on c1.cid = c2.custid where cid>102;

select * from cust_order11 c1 full outer join cust_invoice22 c2 on c1.cid = c2.custid where custid
>102 and cid >102;

Q105......we have two tables A and B

A B

a1 a2 b1 b2

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

create table A(a1 number(5) ,a2 number(5));

create table B(b1 number(5) ,b2 number(5));

1...how many rows are display when we apply inner join?? = 16

select * from A inner join B on A.a1 = B.b2;

2...how many rows are display when we apply left outer join?? = 16

select * from A left outer join B on A.a1 = B.b2;

3...how many rows are display when we apply right outer join?? = 16

select * from A right outer join B on A.a1 = B.b2;

4...how many rows are display when we apply full outer join?? = 16

select * from A full outer join B on A.a1 = B.b2;


Q106.....we have two tables AA and BB

AA BB

a1 a2 b1 b2

1 1 (null) 1

1 (null) 1 (null)

(null) 1 1 1

1 1 1 1

create table AA(a1 number(5) ,a2 number(5));

create table BB(b1 number(5) ,b2 number(5));

1...how many rows are display when we apply inner join?? = 9

select * from AA inner join BB on AA.a1 = BB.b2;

2...how many rows are display when we apply left outer join?? = 10

select * from AA left outer join BB on AA.a1 = BB.b2;

3...how many rows are display when we apply right outer join?? = 10

select * from AA right outer join BB on AA.a1 = BB.b2;

4...how many rows are display when we apply full outer join?? = 11

select * from AA full outer join BB on AA.a1 = BB.b2;

equi join----

it wil display common data from both the table. it will uses where clause to apply the conditions

select * from cust_order11;

select * from cust_invoice22;

select * from cust_order11 c1,cust_invoice22 c2 where c1.cid = c2.custid;


non equi join------

-if we use other than = operator we call it as non equi join

select * from cust_order11 c1 ,cust_invoice22 c2 where c1.cid != c2.custid;

select * from cust_order11 c1, cust_invoice22 c2 where c1.cid > c2.custid;


self join------the table join with itself is known as self join

create table qqqq(cid number(5), cname varchar(20), mid number(5));

select * from qqqq a1 inner join qqqq a2 on a1.cid = a2.mid;

cross join......

it is based on cartesian method.each row of one table will be mapped with every row of the another
table

select * from cust_order11;

select * from cust_invoice22;

select * from cust_order11 c1 cross join cust_invoice22 c2 ;

select * from cust_order11 c1 cross join cust_invoice22 c2 where cid>104;

select * from cust_order11 c1 cross join cust_invoice22 c2 where custid>104;


Set Operator :

Set operators combine the two quries and display the results. or it is used to join the results of two
or more select statemnents

Rules for using set operators:

1. No of columns should be same in both tables

2.Datatypes should be compatible /same for both the tables.

Create table tablea (eid number(5), ename varchar2(20), ecity varchar2(20));

Create table tableb (eid number(5), dname varchar2(20), ecity varchar2(20));

A ----{1,2,3,4,4,4,5,9,10,20}

B ----{1,2,3,4,4,5,5,20,21,22}

A Union B----------{1,2,3,4,5,9,10,20,21,22}

A intersect B------{1,2,3,4,5,20}

A Union All B------{1,2,3,4,4,4,5,9,10,20,1,2,3,4,4,5,5,20,21,22}

A minus B----------{9,10}

B minus A----------{21,22}

Union

It will display the unique records and remove the duplicates from the results.

i.e only one row will listed from duplicate results.

select * from tablea union select * from tableb;........ename

select * from tableb union select * from tablea;........dname

select * from tablea union select * from tablea;

select ename,eid from tablea union select dname,eid from tableb;

select dname,eid from tableb union select ename,eid from tablea;

Union All

It will display all the records i.e unique+duplicate

select * from tablea union all select * from tableb;

select * from tableb union all select * from tablea;


Intersect

It will display common records from 2 tables.

select * from tablea intersect select * from tableb;

select * from tableb intersect select * from tablea;

Minus

it displays the rows present in first query but absent in second query with no duplicates.

select * from tablea minus select * from tableb;

select * from tableb minus select * from tablea;

select count(*) from(select * from tablea minus select * from tableb);

select count(*) from(select * from tableb minus select * from tablea);

Q107..how to display unique rows from the table??

select * from tablea union select * from tablea;

select * from tableb union select * from tableb;

Q 108..how to display duplicate records??

select edept ,count(*) from tyuu group by edept having count(*)>1;

select tyuu.*,rowid from tyuu where rowid not in(select max(rowid) from tyuu group by edept);

Q109..how to delete duplicate records??

delete from tyuu where rowid not in(select max(rowid) from tyuu group by edept);

delete from tyuu where rowid not in(select max(rowid) from tyuu group by edept,eid,ename,emob);

Q110..how to display unique records??

select distinct(edept) from tyuu;

select edept ,count(*) from tyuu group by edept having count(*)=1;

select tyuu.*,rowid from tyuu where rowid in(select max(rowid) from tyuu group by edept);
Q111..can we join 5 tables??

Yes of course, but can you provide me a high level design

select * from cust_order11;

select * from cust_invoice22;

select * from cust_destatus;

select * from fullnamee;

select * from oreturn;

select * from cust_order11 c1 inner join cust_invoice22 c2 on c1.cid = c2.custid inner join
cust_destatus c3 on c1.cid = c3.lid inner join fullnamee c4 on c1.cid = c4.fid inner join oreturn c5 on
c1.cid=c5.rid;

select c1.cid,c1.cname,c1.ccity,c2.oid,c2.oname,c2.billamt,c3.status,c4.fullname , c5.returns from


cust_order11 c1 inner join cust_invoice22 c2 on c1.cid = c2.custid inner join cust_destatus c3 on
c1.cid = c3.lid inner join fullnamee c4 on c1.cid = c4.fid inner join oreturn c5 on c1.cid = c5.rid;
Q112…write a query to select 3rd max salary in department ‘IT’ with employee details

empi:

eid ename esal


101 yusuf 80000
102 amit 75000
103 shivani 90000
104 abhi 50000
105 priya 80000
106 nik 105000
107 sachin 150000
108 suchitra 95000

department:

eid dname
101 cse
102 mech
103 it
104 cse
105 it
106 cse
107 it
108 it

select * from (select eid,dname,ename, esal ,dense_rank() over (order by esal desc) as ranking from
(select c1.eid, c1.ename, c2.dname,c1.esal from empi c1 inner join department c2 on c1.eid =
c2.eid where c2.dname='it' group by c2.dname ,c1.esal, c1.eid ,c1.ename)) where ranking=3 ;

Q112…write a query to select max salary with employee details

select * from (select eid,dname,ename, esal ,dense_rank() over (order by esal desc) as ranking from
(select c1.eid, c1.ename, c2.dname,c1.esal from empi c1 inner join department c2 on c1.eid =
c2.eid )) where ranking=1 ;

Q….how to display 50% data from table?

select * from tyuu;……..this table contain 23 rows

select * from tyuu where rownum<=(select count(eid)/2 from tyuu);…….display 11 rows

if the table contain odd rows then display -1 rows.

select * from ksh10;……..this table contain 6 rows

select * from ksh10 where rownum<=(select count(id)/2 from ksh10);…….display 3 rows


Q….from the following table find out the workres who are in electric things departments

eid Edept Ename

101 Electric-fitter Kshitija

102 Electric-wire Pragati

103 Mech Kunal

104 Electric-bulb Sarthak

105 Cse Pushpak

106 civil sayali

select * from ele where edept like 'electric%';

Q…display departmentwise salary greater than 60000

select esal, edept from tyuu where esal >60000 group by edept,esal ;

Q…display employee details whose joining year is 2016 and 2017

select * from mkl where to_char(doj,'yyyy') =2016 or to_char(doj,'yyyy')=2017 ;

Q…difference between left join and right join

Seq. Left join Right join


no.

1 Left join will display the common right join will display the common
records from both side tables and also records from both side tables and also
display the remaining records from left display the remaining records from
side table. right side table.

For that left side table value it will For that right side table value it will
display null to the right side table. display null to the left side table.

Q…what are different types of datatypes?

Number, char, varchar, varchar2, int, float, unsigned long(4 bytes), unsigned long long(8 bytes)

Q…what are the conditions for using union operator ?

Rules for using set operators: 1. No of columns should be same in both tables

2.Datatypes should be compatible /same for both the table


Q….difference between union and distinct

Seq.no. Union Distinct

1. It is use for two tables It is use for one table

2. It display the unique records It displays unique records from


from both side table and remove particular column of the table
duplicates

3. Select * from tablea union select Select distinct(edept) from emp;


* from tableb;

Q…difference between rowid and rownum

Seq.no. rownum Rowid

1. Rownum is temporary sequence Rowid is permanent sequence number


number

2. Rownum is numeric value Rowid is 16-bit hexadecimal value

3. Rownum is the sequential number Rowid is physical address


allocated to each row during query
execution

4. Rownum is generated at time of select Rowid is generated at the time of


statement insertion of row

5. It gives count of records It gives address of records

Q….difference between union and union all

Seq. no. union Union all

1. It display unique records and remove It displays all the records . i.e. (duplicates
duplicates from the result. + unique)

2. select * from tablea union select * from select * from tablea union all select *
tableb; from tableb;

Q….difference between substring and mainstring

Seq. no. mainstring substring

1. Means whole word Part of the mainstring

2. Ex. ‘Kshitija’ is a maintring Ex. Ksh, shiti, ija, ksh are the substring of
kshitija

Substr() function is use to find substring

You might also like