Sunteți pe pagina 1din 11

Structured Query Language

DISTIDSr. No. 3
Basic SQL Queries
Write SQL queries to do the following:
. Show the structure of the DEPT table.
D!S" D!#T$
2 Select all data from the table.
S!L!"T %
&'() D!#T$
2. Create a query to display unique job codes from the EMP!"EE table.
S!L!"T DISTIN"T *(B
&'() !)#$
#. Create a query to display the last name concatenated with the job $D%
separated by a comma and space% and name the column &Employee and
Title'.
S!L!"T !N+)! ,, -./,, *(B 0!12loyee and Title3
&'() !)#$
(. Create a query to display all the data from the employee table. Separate
each column by a comma. )ame the column T*E+!,TP,T.
S!L!"T !)#N(,, -./ ,, !N+)! ,, -./ ,, *(B ,, -./ ,, )4' ,, -./ ,, S+L,,
-./ ,, D!#TN( ,,/./,, "()) //(5T6#5T3&'() !)#$
-. Create a query to display the last name and salary of employee earnin.s
more than /2%000.
S!L!"T !N+)!. S+L
&'() !)#
W7!'! S+L 8 9:::$
;. Create a query to display the employee last name and department number
for employee number /2 .
S!L!"T !N+)!.D!#TN(
&'() !)#
W7!'! !)#N(<===$
>. Create a query to display the last name and salary for all EMP whose
salary is not in the ran.e of -%000 and /2%000.
S!L!"T !N+)!. S+L
&'() !)#
W7!'! S+L N(T B!TW!!N =::: +ND 9:::$
1. Create a query to display the employee last name% job $D% and start date
of employees hired between 2ebruary 20% /331 and May /% /331. !rder
the query in ascendin. order by start date.

DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
S!L!"T !N+)!. *(B. 7I'!D+T!
&'() !)#
W7!'! 7I'!D+T! B!TW!!N /9:?&e@?AAB/ +ND /:?)ay?AAB/
('D!' BC 7I'!D+T!$
3. Create a query to display the last name and department number of all the
employees in the department 20 and -0 in alphabetical order by name.
S!L!"T !N+)!. D!#TN(
&'() !)#
W7!'! D!#TN( IN D9:.=:E
('D!' BC !N+)!$
/0. Create a query to display the last name and salary for all employees
whose salary is in the ran.e of -%000 and /2%000 and are in the
department 20 or -0. abel the columns Employee and Monthly Salary%
respecti4ely.
S!L!"T !N+)! 0!12loyee3. S+L 0)onthly Salary3
&'() !)#
W7!'! S+L B!TW!!N =::: +ND 9:::
+ND D!#TN( IN D9:.=:E$
//. Create a query to display the last name and hire date of e4ery employee
who was hired in /33(.
S!L!"T !N+)!. 7I'!D+T!
&'() !)#
W7!'! 7I'!D+T! LIF! -GAH/$
/2. Create a query to display the last name and job title of all employees who
do not ha4e a mana.er.
S!L!"T !N+)!. *(B
&'() !)#
W7!'! )4' IS N5LL$
/#. Create a query to display the last name% salary% and commission for all
employees who earn commissions. Sort the data in descendin. order of
salary and commissions.
S!L!"T !N+)!. S+L. "())
&'() !)#
W7!'! "()) IS N(T N5LL
('D!' BC S+L D!S". "()) D!S"$
H. Create a query to display the last name of all employees where the third
letter of the name is an a.
S!L!"T !N+)!
&'() !)#
W7!'! !N+)! LIF! -66aG/$
9 DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
/-. Create a query to display the last name of all the employees who ha4e an
a and an e in their last names.
S!L!"T !N+)!
&'() !)#
W7!'! !N+)! LIF! -GaG/
+ND !N+)! LIF! -GeG/$
/5. Create a query to display the last name% job title% and salary for all
employees whose job is sales representati4e or stoc6 cler6 and whose
salary is not equal to 72-00% 7#-00% or 78000.
S!L!"T !N+)!. *(B. S+L
&'() !)#
W7!'! *(B IN D-S+L!S)+N/./"L!'F/E
+ND S+L N(T IN D9=::.3=::.>:::E$
Sr. No. H
Single 'ow &unctions
Write SQL queries to do the following:
/. 9rite a query to display the current date. abel the column Date.
S!L!"T SCSD+T! 0Date3
&'() dual$
2. Create a query to display the employee number% last name% salary% and
salary increased by /-: and e;pressed as a whole number. abel the
column )ew Salary.
S!L!"T !)#N(. !N+)!. S+L.'(5NDDS+L % .=.:E 0New Salary3
&'() !)#$
#. $n the abo4e query add a column that subtracts the old salary from the
new salary. abel the column $ncrease.
S!L!"T !)#N(. !N+)!. S+L.
'(5NDDS+L % .=.:E 0New Salary3.
'(5NDDS+L% .=.:E I S+L 0Increase3
&'() !)#$
(. 9rite a query that displays the employee's last name with the first letters
capitali<ed and all other letters lowercase% and the len.th of the names%
for all employees whose name starts with =% >% or M. ?i4e each column
an appropriate label. Sort the result by the employees' last names.
S!L!"T INIT"+#D!N+)!E 0Na1e3.
L!N4T7D!N+)!E 0Length3
&'() !)#
W7!'! !N+)! LIF! -*G/ ('
!N+)! LIF! -+G/ ('
!N+)! LIF! -)G/
('D!' BC !N+)!$
3 DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
-. 2or each employee% display the employee's last name% and calculate the
number of months between today and the date the employee was hired.
abel the column M!)T*S+9!@AED. !rder your result by the
number of months employed. @ound the number of months up to the
closest whole number.
S!L!"T !N+)!. '(5NDD)(NT7S6B!TW!!NDSCSD+T!.
7I'!D+T!EE )(NT7S6W('F!D
&'() !)#
('D!' BC )(NT7S6B!TW!!NDSCSD+T!. 7I'!D+T!E$
5. 9rite a query that produces the followin. for each employeeB Cemployee
last nameD earns CsalaryD monthly but wants C# times salaryD. abel the
column Dream Salaries.
S!L!"T !N+)! ,, - earns -
,, T(6"7+'DS+L./&)JAA.AAA.::/E
,, - 1onthly @ut wants/
,, T(6"7+'DS+L % 3./&)JAA.AAA.::/E
,, -./ 0Drea1 Salaries3
&'() !)#$
8. Create a query to display the last name and salary for all employees.
2ormat the salary to be /- characters lon.% leftEpadded with 7. abel the
column S>>@".
S!L!"T !N+)!. L#+DDS+L. =. -J/E S+L
&'() !)#$
1. Display each employee's last name% hire date% and salary re4iew date%
which is the first Monday after si; months of ser4ice. abel the column
@EF$E9. 2ormat the dates to appear in the format similar to GMonday%
the ThirtyE2irst of =uly% 2000.H
S!L!"T !N+)!. 7I'!D+T!.
T(6"7+'DN!KT6D+CD+DD6)(NT7SD7I'!D+T!.;E./)(ND+C/E.
f1Day. LtheL Dds2th LofL )onth./ CCCC/E '!MI!W
&'() !)#$
3. Display the last name% hire date% and day of the wee6 on which the
employee started wor6in.. abel the column D>". !rder the results by
the day of the wee6 startin. with Monday.
S!L!"T !N+)!. 7I'!D+T!. T(6"7+'D7I'!D+T!./D+C/E D+C
&'() !)#
('D!' BC T(6"7+'D7I'!D+T!?./d/E$
/0. Create a query that displays the employees' last name and commission
amounts. $f an employee does not earn commission% put G)o
CommissionH. abel the column C!MM.
S!L!"T !N+)!. NMLDT(6"7+'D"())E. -No "o11ission/E
"())
&'() !)#$
H DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
Sr. No. =
5sing )ulti2le Ta@les
Write SQL queries to do the following:
= DB)S
/. 9rite a query to display the last name% department number% and
department name for all employees.
S!L!"T !.!N+)!. !.D!#TN(. D.DN+)!
&'() !)# !. D!#T D
W7!'! !.D!#TN( <D.D!#TN($
9. Create a unique listin. of all jobs that are in department 20. $nclude the
location of the department in the output.
S!L!"T DISTIN"T *(B . L("
&'() !)# !. D!#T D
W7!'! !.D!#TN( <D.D!#TN(
+ND !.D!#TN(<9:$
3. 9rite a query to display the employee last name% department name%
location% and city of all employees who earn a commission.
S!L!"T !.!N+)!. D.D!#TN(. D.L(". L."ITC
&'() !)# !. D!#T D. L(" L
W7!'! !.D!#TN( <D.D!#TN(
+ND D.L("<L."ITC
+ND !."()) IS N(T N5LL$
H. Display the employee last name and department name for all employees
who ha4e an a IlowercaseJ in their last names.
S!L!"T !.!N+)!. D.DN+)!
&'() !)# !. D!#T D
W7!'! !.D!#TN( <D.D!#TN(
+ND !N+)! LIF! -GaG/$
-. 9rite a query to display the last name% job% department number% and
department name for all employees who wor6 in Toronto.
S!L!"T !.!N+)!. !.*(B. !.D!#TN(. D.DN+)!
&'() !)# ! *(IN D!#T D
(N D!.D!#TN( <D.D!#TN(E
*(IN L(" L
(N DD.L("<L."ITCE
W7!'! L(W!'DL."ITCE < -toronto/$
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
; DB)S
5. Display the names and hire dates for all employees who were hired
before their mana.ers% alon. with their mana.er's name and hire date.
abel the columns Employee% Emp. *ired% Mana.er% M.r *ired%
respecti4ely.
S!L!"T W.!N+)!. W.7I'!6D+T!. ).!N+)!. ).7I'!D+T!
&'() !)# W. !)# )
W7!'! W.)+N+4!'6ID<).!)#L(C!!6ID
+ND W.7I'!6D+T!N).7I'!6D+T!$
8. Create a query that displays employee last names% department numbers%
and all the employees who wor6 in the same department as a .i4en
employee.
S!L!"T !.!N+)!. !.D!#TN(
&'() !)# !. D!#T D
W7!'! !.D!#TN(<D.D!#TN($
1. Create a query that displays the name% job% department name% salary% and
.rade for all the employees.
S!L!"T !.!N+)! .!.*(B.!.S+L.!.D!#TN(.!.)4'
&'() !)# !. D!#T D
W7!'! !.D!#TN(<D.D!#TN($
3. Create a query to display the name and hire date of any employee hired
after employee Da4ies.
S!L!"T !.!N+)!. !.7I'!D+T!
&'() !)# !
W7!'! !.!N+)! LIF!<
/0. Display the names and hire dates for all employees who were hired
before their mana.ers alon. with their mana.er's name and hire dates.
abel the column Employee% Emp. *ired% Mana.er% and M.r *ired%
respecti4ely.
>)S 5
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
Sr. No. ;
4rou2 &unctions
Write SQL queries to do the following:
/. Display the hi.hest% lowest% sum% and a4era.e salary of all employees.
abel the columns Ma;imum% Minimum% Sum% and >4era.e% respecti4ely.
@ound your result to the nearest whole number.
S!L!"T '(5NDD)+KDS+LE.:E 0)aOi1u13.
'(5NDD)INDS+LE.:E0)ini1u13.
'(5NDDS5)DS+LE.:E 0Su13.
'(5NDD+M4DS+LE.:E0+Perage3
&'() !)#
4'(5# BC *(B$
2. Display the hi.hest% lowest% sum% and a4era.e salary for each job type.
abel the columns =ob $D% Ma;imum% Minimum% Sum% and >4era.e%
respecti4ely. @ound your result to the nearest whole number.
S!L!"T *(B6ID. '(5NDD)+KDS+L+'C.:E 0)aOi1u13.
'(5NDD)INDS+L+'C.:E 0)ini1u13.
'(5NDDS5)DS+L+'C.:E 0Su13.
'(5NDD+M4DS+L+'C.:E 0+Perage3.
&'() !)#
4'(5# BC *(B6ID$
#. 9rite a query to display the number of people with the same job.
S!L!"T *(B. "(5NTD%E
&'() !)#
4'(5# BC *(B$
H. Determine the number of man.ers without listin. them. abel the column
)umber of Mana.ers. *int ,se the Mana.er+id column to determine the
number of mana.er.
S!L!"T "(5NTDDISTIN"T )4'E 0No. of )anagers3
&'() !)#$
-. 9rite a query that displays the difference between the hi.hest and lowest
salaries. abel the column D$22E@E)CE.
S!L!"T )+KDS+LE I )INDS+LE 0 DI&&!'!N"!3
&'() !)#$
;. Display the man.er number and the salary of the lowest paid employee for
that mana.er. E;clude anyone whose mana.er is not 6nown.Iie include
where mana.er is 6nownJ E;clude any .roups where the minimum salary
is 75%000 or less.Iie include min salary is .reater than 5000J.Sort the
output in descendin. order of salary.
> DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
S!L!"T )4'.)INDS+LE
&'() !)#
W7!'! )4' IS N(T N5LL
4'(5# BC )4'
7+MIN4 )INDS+LE 8 ;:::
('D!' BC )INDS+LE D!S"$
B DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
8. 9rite a query to display each department's name% location% number of
employees% and the a4era.e salary for all employees in that department.
abel the columns )ame% ocation% )umber of People% and salary. @ound
the a4era.e salary to two decimal places.
S!L!"T D.DN+)! 0Na1e3. D.L(" 0Location3. "(5NTD%E 0No. of
#eo2le3.
'(5NDD+M4DS+LE.9E 0Salary3
&'() !)# !. D!#T D
W7!'! !.D!#TN(<D.D!#TN(
4'(5# BC D!#TN($
1. Create a query that will display the total number of employees and% of that
total% the number of employees hired in /33-% /335% /338% and /331.
Create appropriate headin.s.
S!L!"T "(5NTD%E T(T+L.
S5)DD!"(D!DT(6"7+'D7I'!6D+T!./CCCC/E.AA=..:EE 0AA=3.
S5)DD!"(D!DT(6"7+'D7I'!6D+T!./CCCC/E.AA;..:EE 0AA;3.
S5)DD!"(D!DT(6"7+'D7I'!6D+T!./CCCC/E.AA>..:EE 0AA>3.
S5)DD!"(D!DT(6"7+'D7I'!6D+T!./CCCC/E.AAB..:EE 0AAB3.
&'() !)#$
A DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
Sr. No. >
Su@queries
Write SQL queries to do the following:
/. 9rite a query to display the last name and hire date of any employees in
the same department as Kolt6ey. E;clude Kolt6ey.
S!L!"T !N+)!. 7I'!D+T!
&'() !)#
W7!'! D!#TN( < DS!L!"T D!#TN(
&'() !)#
W7!'! !N+)!</QoltRey/E
+ND !N+)! N 8 -QoltRey/$
2. Create a query to display the employee numbers and last names of all
employees who earn more than the a4era.e salary. Sort the results in
ascendin. order of salary.
S!L!"T !)#N(. !N+)!.S+L
&'() !)#
W7!'! S+L8 DS!L!"T +M4DS+LE
&'() !)#E
('D!' BC S+L$
#. 9rite a query that displays the employee numbers and last names of all the
employees who wor6 in a department with any employee whose last name
contains a u.
S!L!"T !)#N(. !N+)!
&'() !)#
W7!'! D!#TN( IN DS!L!"T D!#TN(
&'() !)#
W7!'! !N+)! LIF! -5G/E$
(. Display the last name% department number% and job id% of all employees
whose department location id is L!ST!).
S!L!"T !N+)!. D!#TN(. *(B
&'() !)#
W7!'! D!#TN( IN DS!L!"T D!#TN(
&'() D!#T
W7!'! L("</B(ST(N/E$
-. Display the last name and salary of e4ery employee who reports to Ain..
S!L!"T !N+)!. S+L
&'() !)#
W7!'! )4' < DS!L!"T !)#N(
&'() !)#
W7!'! !N+)!</Fing/E$
: DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
Structured Query Language
5. Display the department number% last name% and job for e4ery employee in
the e;ecuti4e department.
S!L!"T D!#TN(. !N+)!. *(B
&'() !)#
W7!'! D!#TN( IN DS!L!"T D!#TN(
&'() D!#T
W7!'! DN+)! < -!OecutiPe/E$
8. Show the department number and the lowest salary of the department with
the hi.hest a4era.e salary.
S!L!"T D!#TN(. )INDS+LE
&'() !)#
4'(5# BC D!#TN(
7+MIN4 +M4DS+LE < DS!L!"T )+KD+M4DS+LEE
&'() !)#
4'(5# BC D!#TN(E$
1. Show the department numbers% department name% and the number of
employees wor6in. in each department thatB
a. $ncludes fewer than ten employees.
S!L!"T D.D!#TN(. D.DN+)!. "(5NTD%E
&'() D!#T D. !)# !
W7!'! D.D!#TN( < !.D!#TN(
4'(5# BC D.D!#TN(. D.DN+)!
7+MIN4 "(5NTD%E N :$
@. *as the hi.hest number of EMP.
S!L!"T D.D!#TN(. D.DN+)!. "(5NTD%E
&'() D!#T D. !)# !
W7!'! D.D!#TN( < !.D!#TN(
4'(5# BC D.D!#TN(. D.DN+)!
7+MIN4 "(5NTD%E < DS!L!"T )+KD"(5NTD%EE
&'() !)#
4'(5# BC D.D!#TN(E$
c. *as the lowest number of employees.
S!L!"T D.D!#TN(. D.DN+)!. "(5NTD%E
&'() D!#T D. !)# !
W7!'! D.D!#TN( < !.D!#TN(
4'(5# BC D.D!#TN(. D.DN+)!
7+MIN4 "(5NTD%E < DS!L!"T )IND"(5NTD%EE
&'() !)#
4'(5# BC D.D!#TN(E$

DB)S
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net

S-ar putea să vă placă și