SQL Interview Questions
SQL Interview Questions
3. Constraint validation/Testing
By using Joins, Aggreagte Functions, Rank and dense rank validated different type of data.
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.
1.ddl-----alter,truncate,drop,rename,create
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
Update is use for updating the particular row of the table and also updating particular
column data.
3.dcl------grant,revoke
4.tcl------commit,rollback
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;
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
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
Describe tablename;
By using is null keyword. Is null keyword is only use with where clause.
Select * from emp where eid is not null;…..is not null keyword
Q14.how to update salary with 12000 from table whose ename is ‘kshitija’?
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
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;
select (case when gender='m' then 'male' else 'female' end) as genderfind from qqqqqqqqqq;
Seq.no between In
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’;
select * from logic where firstname like '%a_ _';.......................................third last charcater is a
select * from logic where firstname like '%';.............................unknown length of the string
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?
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
Here eid column does not accepts Here cname and cadd column will not accepts
duplicate and null values. duplicate value but accepts null values.
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
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
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
select count(*) from aggre;……this will count the no of rows of the table..it contain null also
substr(columnname,x,y)
select substr(cname,1,4) from ksh10;.......finding substring from each name of the column cname;
instr-----this function will return the location of substring from the string
select instr('sangharsha','h') from dual;......this query will return the location of first 'h' of string.....5
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',-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
pname values:TV123456789LG101
TV198765435Samsung199
TV198765435Samsung201
TV123456789Onida878
Output:
productno_modelno
123456789_101
198765435_199
198765435_201
123456789_878
Answer:
input:
column name:email
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:
add_months......
months_between-----
select months_between(doj,dor) from datee;...........it will give negative value i.e. months
between doj and dor is in negative
next_day..........
select next_day(doj,'mon') from datee;.......display next monday of each doj from table datee
last_day.........
sysdate........
systimestamp.......
to_date………
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
Q40. can you display employees joinning year along with ename??
select * from mkl where to_char(doj,'yyyy')=2019;.displaying records whoes joining year is 2019
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
Q46.displaying employee data who join on between given dates ‘05-25-2017’ and ‘jan-01-2019’
using date_function??
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'));
Q49.display the employee information who join on partucular date(apr-30-2012) using date
function??
Q50.what is NVL??
Q51.what is decode??
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',
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.
Q54.group by clause
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 from column
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 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
105 pd 125000
107 pd 40000
Q62.having clause
It applies filter on results which are generated by group by clause using specific aggregate
function.
asc....0 to 9, a to z, A to Z
desc...9 to 0,z to a, Z to A
group by edept
having count(*)>1
group by edept
group by edept
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
1. From
2. Where
3. Group by
4. Having
5. Select
6. Order by
This command is use to change column name,datatype, size and adding and deleting the
column from existing table.
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
select * from aggre where esal= (select min (esal) from aggre);
Q79..pseudo columns
1.rank()
2.dense_rank()
3.rownum 4.rowid
Q80. Difference between rank() and dense_rank()
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
select * from (select tyuu.*, dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking =1;
select *from (select tyuu.*, dense_rank() over(order by esal asc nulls last ) as ranking from
tyuu)where ranking =1;
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;
select * from (select tyuu.*, dense_rank() over(order by esal desc nulls last) as ranking from
tyuu) where ranking=3;
select * from (select tyuu.*, dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking=4;
select * from (select tyuu.*, dense_rank() over(order by esal desc nulls last) as ranking from tyuu)
where ranking =64;
select * from (select tyuu.*,dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking=1000;
select * from (select tyuu.*,dense_rank() over (order by esal desc nulls last) as ranking from
tyuu) where ranking <=5;
select * from (select tyuu.*,dense_rank() over(order by esal desc nulls last) as ranking from tyuu)
where ranking between 3 and 6;
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);
select * from (select tyuu.*,dense_rank() over(order by esal asc nulls last) as ranking from tyuu)
where ranking =4;
Q94…rownum
select tyuu.*,rownum from tyuu where rownum <=5 order by rownum asc nulls last ;
select * from (select tyuu.*,rownum as seqno from tyuu order by seqno desc) where rownum
<=5 order by seqno ;
Q97…rowid
select * from tyuu where rowid=(select max(rowid) from tyuu where edept='cse');
Ans: no
inner join....It dispalys the matching records(common data) from both side table based on specific
condition.
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 where c1.cid
<=102;.....display common data from both table having cid <=102
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.
select * from cust_order11 c1 left 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 cid
>102;...
select * from cust_order11 c1 right outer join cust_invoice22 c2 on c1.cid = c2.custid where custid
>102;...
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 where custid
>102 and cid >102;
A B
a1 a2 b1 b2
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
2...how many rows are display when we apply left outer join?? = 16
3...how many rows are display when we apply right outer join?? = 16
4...how many rows are display when we apply full outer join?? = 16
AA BB
a1 a2 b1 b2
1 1 (null) 1
1 (null) 1 (null)
(null) 1 1 1
1 1 1 1
2...how many rows are display when we apply left outer join?? = 10
3...how many rows are display when we apply right outer join?? = 10
4...how many rows are display when we apply full outer join?? = 11
equi join----
it wil display common data from both the table. it will uses where clause to apply the conditions
cross join......
it is based on cartesian method.each row of one table will be mapped with every row of the another
table
Set operators combine the two quries and display the results. or it is used to join the results of two
or more select statemnents
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 minus B----------{9,10}
B minus A----------{21,22}
Union
It will display the unique records and remove the duplicates from the results.
Union All
Minus
it displays the rows present in first query but absent in second query with no duplicates.
select tyuu.*,rowid 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);
delete from tyuu where rowid not in(select max(rowid) from tyuu group by edept,eid,ename,emob);
select tyuu.*,rowid from tyuu where rowid in(select max(rowid) from tyuu group by edept);
Q111..can we join 5 tables??
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;
empi:
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 ;
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 ;
select esal, edept from tyuu where esal >60000 group by edept,esal ;
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.
Number, char, varchar, varchar2, int, float, unsigned long(4 bytes), unsigned long long(8 bytes)
Rules for using set operators: 1. No of columns should be same in both tables
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;
2. Ex. ‘Kshitija’ is a maintring Ex. Ksh, shiti, ija, ksh are the substring of
kshitija