You are on page 1of 7

Answers to SQL Practice Questions

ANSWERS: (A) Create/Alter/Drop Table Commands:


CREATE TABLE EMP (emp_num int , emp_fname char(2 ! "#T$ %E&A'LT (un)nown( , emp_*name char(2 ! +,T +'LL , -o._c*ass char(/! , hire0ate 0ate +,T +'LL , C,+STRA#+T emp_p) PR#MAR1 2E1 (emp_num! , C,+STRA#+T -o._f) &,RE#3+ 2E1 (-o._c*ass! RE&ERE+CES -o. (-o._co0e! C,+STRA#+T emp_*name_u) '+#Q'E ( emp_*name ! , C,+STRA#+T hire0ate_c) C$EC2 ( hire0ate 45 (6767688 ( ! ! 29 CREATE TABLE :,B ( -o._co0e char( / ! , -o._c*ass char( / ! , ch;_hour 0ecima* (<,2! "#T$ %E&A'LT 6< , C,+STRA#+T -o._p) PR#MAR1 2E1 (-o._co0e! , C,+STRA#+T -o._c*ass_c) C$EC2 ( ch;_hour 45 6< ! ! /9 ALTER TABLE -o. A%% C,L'M+ o=ertimeChar;e 0ec (>,2! +,T +'LL ALTER TABLE -o. A%% C,+STRA#+T char;e_c) C$EC2( ch;_hour 45 2< ! ALTER TABLE -o. ALTER co*umn :o._C*ass set 0efau*t (+A(

?9

<9

>9

ALTER TABLE -o. ALTER co*umn Ch;_$our set +,T +'LL ALTER TABLE -o. ALTER co*umn Ch;_$our set 0ata tApe 0ecima* (62,2! ALTER TABLE -o. %R,P constraint

@9

B9

char;e_c)

89

ALTER TABLE -o. %R,P C,L'M+ o=ertimeChar;e ALTER TABLE -o. %R,P C,L'M+ ch;_hour %R,P TABLE -o. %R,P TABLE emp

6 9

669 629

ANSWERS: (B) Data Manipulation Commands:


69 #nsert the *ast / rows shown into the EMP ta.*e9 #+SERT into emp ( emp_num, emp_fname, emp_*name, -o._c*ass, hire0ate ! CAL'ES ( 6 >, ("i**iam(, (Smithfie*0(, (PR3(, (6 7/672 2( !, #+SERT into emp ( emp_num, emp_fname, emp_*name ! CAL'ES ( 66?, (Anne*ise(, (:ones( ! #+SERT into emp ( emp_num, emp_*name ! CAL'ES ( 66B, (&rommer( !

29 'p0ate the emp*oAee fname to :im instea0 of :ames for emp*oAee 66B (Note: if no WHERE clause is used, then all records are updated) 'P%ATE emp SET emp_fname 5 (:im( "$ERE emp_num 5 66B /9 'p0ate the %ata.ase %esi;ner ch;_hour =a*ue .A 6 D 'P%ATE -o. SET ch;_hour 5 ch;_hour E 696 "$ERE -o._c*ass 5 (%ata.ase %esi;ner(

?9 Remo=e the row from the -o. ta.*e for -o. co0e PR3 ( Note: if no WHERE clause is used, then all records are deleted ) %ELETE &R,M -o. "$ERE -o._co0e 5 (PR3(

<9 Remo=e the :,B ta.*e an0 a** of it(s 0ata %R,P TABLE -o.

>9 Remo=e the emp*oAees from the emp*oAee ta.*e "$ERE the hire0ate is .efore 6F:anF8 %ELETE &R,M emp "$ERE hire0ate G (6F:anF8 (

@9 Remo=e the emp ta.*e an0 a** of it(s 0ata %R,P TABLE emp

ANSWER: (C) Select Commands:


69 Show the emp*oAee first an0 *ast names an0 their -o. c*ass 0escription an0 hour*A char;e9 SELECT emp9emp_fname , emp9emp_*name , -o.9-o._c*ass , -o.9ch;_hour emp, -o. emp9-o._c*ass 5 -o.9-o._co0e

&R,M "$ERE

29 Same Huestion as a.o=e, .ut on*A show the recor0s "$ERE the ch;_hour is G6 SELECT emp9emp_fname , emp9emp_*name , -o.9-o._c*ass , -o.9ch;_hour emp, -o. emp9-o._c*ass 5 -o.9-o._co0e -o.9ch;_hour 5 G 6

&R,M "$ERE A+%

/9 Se*ect the hours wor)e0 an0 *astname an0 hire0ate for a** emp*oAees9 (Note: the default for all selects is to show all records that are joined. The above state ent has no further conditions for the WHERE clause) SELECT &R,M "$ERE emp9emp_*name , emp9hire0ate , pro-_emp9hour emp, pro-_emp emp9emp_num 5 pro-_emp9emp_num

?9 Create a =iew ca**e0I ",R2_#+&, which wi** se*ect the hours wor)e0 an0 *astname an0 hire0ate for a** emp*oAees hire0 .efore 6F-anF 29 (Note: the default for all selects is to show all records that are joined. The above state ent has no further conditions for the WHERE clause) CREATE C#E" ",R2_#+&, as SELECT emp9emp_*name , emp9hire0ate , pro-_emp9hour &R,M emp, pro-_emp "$ERE emp9emp_num 5 pro-_emp9emp_num A+% emp9hire0ate G (6F-anF 2( <9 Se*ect emp*oAee fname an0 *name an0 a** pro-ects that theA are wor)in; on9 Sa=e this se*ect statement as a =iew ca**e0I EMP_PR,:ECTS (Note: the default for all selects is to show all records that are joined. The above state ent has no further conditions for the WHERE clause) CREATE C#E" EMP_PR,:ECTS as SELECT emp9emp_fname , emp9emp_*name , pro-ect9pro-_name &R,M emp, pro-ect , pro-_emp "$ERE pro-_emp9pro-_num 5 pro-ect9pro-_num A+% pro-_emp9emp_num 5 emp9emp_num >9 Se*ect emp*oAee fname an0 *name an0 a** pro-ects that theA are wor)in; on for pro-ect 6B9 SELECT &R,M "$ERE A+% A+% emp9emp_fname , emp9emp_*name , pro-ect9pro-_name emp, pro-ect , pro-_emp pro-_emp9pro-_num 5 pro-ect9pro-_num pro-_emp9emp_num 5 emp9emp_num pro-ect9pro-_num 5 6B

@9 Se*ect a** pro-ect names an0 -o. c*ass 0escriptions that the pro-ect reHuires9 Sa=e this se*ect statement as a =iew ca**e0I EMP_:,B_S2#LLS CREATE C#E" EMP_:,B_S2#LLS as SELECT pro-ect9pro-_name , -o.9-o._c*ass &R,M pro-ect , -o. , emp, pro-_emp "$ERE pro-_emp9pro-_num 5 pro-ect9pro-_num A+% pro-_emp9emp_num 5 emp9emp_num A+% emp9-o._c*ass 5 -o.9-o._co0e

B9 Se*ect a** pro-ect names an0 -o. c*ass 0escriptions that the pro-ect reHuires for on*A the -o. co0e of %B%9 Sa=e this statement as a =iew ca**e0I %ATABASE_%ES#3+ERS CREATE C#E" %ATABASE_%ES#3+ERS as SELECT pro-ect9pro-_name , -o.9-o._c*ass &R,M pro-ect , -o. , emp, pro-_emp "$ERE pro-_emp9pro-_num 5 pro-ect9pro-_num A+% pro-_emp9emp_num 5 emp9emp_num A+% emp9-o._c*ass 5 -o.9-o._co0e A+% emp9-o._c*ass 5 (%B%(

89 Se*ect customer first an0 *ast name, cre0it *imit, an0 sa*esrep first an0 *ast name "$ERE the cre0it *imit is 45 6< 9 SELECT customer9first_name, customer9*ast_name, customer9cre0it_*imit, sa*esrep9*ast_name as sa*esLast+ame, sa*esrep9first_name as sa*es&irst+ame &R,M premiere7customer, premiere7sa*esrep "$ERE customer9cre0it_*imit 45 6<

You might also like