SQL Sample Questions and Answers
SQL Sample Questions and Answers
AntiqueOwners OwnerID OwnerLastName 01 Jones 02 Smith 15 Lawson 21 Akins 50 Fowler Orders OwnerID 02 02 21 15
Antiques SellerID 01 02 15 21 50 01 02 15 01 02 21 50
BuyerID 50 15 02 50 01 21 21 50 15 21 02 01
Item Be Table Chair Mirror Desk Cabinet Co!!ee Table Chair Jewelr" Bo# Potter" Bookcase Plant Stan
EmployeeAddressTable Address %( First Street %'2 +ine A,e(( /lm St''0 2-S- 110
EmployeeIDNo
Salary
EmployeeStatisti!sTable Benefits
"osition
(-
'5$&%.-
10- CREATE
11- CREATE TABLE ORDERS (OWNERID INTE!ER NOT NULL, ITEMDESIRED CHAR(.() NOT NULL); 12- U"DATE ANTIQUEOWNERS SET OWNERFIRSTNAME = '/0h#' WHERE OWNERID = (SELECT BUYERID FROM ANTIQUES WHERE ITEM = 'B0012a3%'); 1(- SELECT OWNERFIRSTNAME, OWNERLASTNAME FROM ANTIQUEOWNERS WHERE E-ISTS (SELECT 4 FROM ANTIQUES WHERE ITEM = 'Chair'); 1'- SELECT OWNERID, 'i3 i# 506h Or7%r3 8 A#6i9:%3' FROM ORDERS, ANTIQUES WHERE OWNERID = BUYERID UNION SELECT BUYERID, 'i3 i# A#6i9:%3 0#;<' FROM ANTIQUES WHERE BUYERID NOT IN (SELECT OWNERID FROM ORDERS); 15- SELECT 4 FROM ANTIQUEOWNERS, ORDERS;
2sin4 the Sam3le tables8 write a S6L statement to7 1- Show each Anti9:es or er an the last an !irst names o! the 3erson who or ere the itemSELECT A#6i9:%O=#%r3.O=#%rLa36Na>%, A#6i9:%O=#%r3.O=#%rFir36Na>%, Or7%r3.I6%>D%3ir%7 FROM A#6i9:%O=#%r3, Or7%r3 WHERE A#6i9:%O=#%r3.O=#%rID = Or7%r3.O=#%rID; 0r SELECT A#6i9:%O=#%r3.O=#%rLa36Na>%, A#6i9:%O=#%r3.O=#%rFir36Na>%, Or7%r3.I6%>D%3ir%7 FROM A#6i9:%O=#%r3 RI!HT /OIN Or7%r3 ON A#6i9:%O=#%r3.O=#%rID = Or7%r3.O=#%rID;
2- Show each col:mn in the /m3lo"eeStatisticsTable in al3habetical or er b" Position8 then b" /m3lo"ee;D0oSELECT 4 FROM E>?;0<%%S6a6i36i23Ta5;% ORDER BY "03i6i0#, E>?;0<%%IDN0;
(- Show the ann:al b: 4et !or Bene!its !rom the /m3lo"eeStatisticsTableSELECT S:>(B%#%@i63) FROM E>?;0<%%S6a6i36i23Ta5;%;
'- 2sin4 the ;0 *3erator8 show the names o! the owners o! ChairsSELECT O=#%rLa36Na>%, O=#%rFir36Na>% FROM A#6i9:%O=#%r3, A#6i9:%3 WHERE I6%> I# ('Chair') AND A#6i9:%O=#%r3.O=#%rID = A#6i9:%3.B:<%rID;
5- Show the names o! all Anti9:es *wners who ha,e o not ha,e an or er 3lace SELECT O=#%rLa36Na>%, O=#%rFir36Na>% FROM A#6i9:%O=#%r3 WHERE O=#%rID NOT IN (SELECT O=#%rID FROM Or7%r3);
$- Show the names o! those who ha,e 3lace Anti9:e or ers8 with no :3licatesSELECT DISTINCT O=#%rLa36Na>%, O=#%rFir36Na>% FROM Or7%r3, A#6i9:%O=#%r3 WHERE A#6i9:%O=#%r3.O=#%rID = Or7%r3.O=#%rID; 0r 60 :3% /OIN #06a6i0#A SELECT DISTINCT A#6i9:%O=#%r3.O=#%rLa36Na>%, A#6i9:%O=#%r3.O=#%rFir36Na>% FROM A#6i9:%O=#%r3 RI!HT /OIN Or7%r3 ON A#6i9:%O=#%r3.O=#%rID = Or7%r3.O=#%rID;
&- Delete all o! Bob Smith<s Anti9:e or ersDELETE FROM ORDERS WHERE OWNERID = (B;
%- Create an Anti9:e or er !or a =ockin4 Chair !or Jane AkinsINSERT INTO ORDERS ALUES (B), 'R021i#$ Chair');
.- Create a table calle /m3lo"ees8 with col:mns /m3lo"ee;D0o8 First0ame8 an Last0ameCREATE TABLE EM"LOYEES (E>?;0<%%IDN0 INTE!ER NOT NULL, Fir36Na>% CHAR(.() NOT NULL, La36Na>% CHAR(.() NOT NULL);
10- Show the ann:al b: 4et !or Salar" b" each 3osition !rom the /m3lo"eeStatisticsTable
SELECT "03i6i0#, S:>(Sa;ar<) FROM E>?;0<%%S6a6i36i23Ta5;% !ROU" BY "03i6i0#;
11- >hat is the relationshi3 between the Anti9:e*wners table an the *wners table? *ne to one 12- ;! "o: o not ha,e a 3rimar" ke" in a table8 the a ition o! what t"3e o! col:mn is 3re!erre to 4i,e the table a 3rimar" ke"? An inte4er i enti!ication n:mber@ an a:to5increment ;D is 3re!erre 1(- >hich !:nction will allow "o: to s:bstit:te a 4i,en ,al:e !or an" 0:ll ,al:es arisin4 !rom a Select statement? 0+L 1'- ;! all o! the col:mns in all o! the tables in "o:r schema are e3en ent solel" on the ,al:e o! the 3rimar" ke" in each table8 in which 0ormal Form is "o:r esi4n? Thir 0ormal Form 15- >hat term is :se to escribe the e,ent o! a atabase s"stem a:tomaticall" :3 atin4 the ,al:es o! !orei4n ke"s in other tables8 when the ,al:e o! a 3rimar" ke" is :3 ate ? Casca in4 :3 ate 1$- >hat atabase obAect 3ro,i es !ast access to the ata in the rows o! a table? ;0D/B 1&- >hat t"3e o! S6L statement is :se to chan4e the attrib:tes o! a col:mn?
ALTER TABLE.
1%- ;n a Create Table statement8 when a col:mn is esi4nate as 0*T 02LL8 what oes this mean? A ,al:e is re9:ire in this col:mn !or e,er" row in the table
20- ;! "o: wish to write a 9:er" that is base on other 9:eries8 rather than tables8 what o these other 9:eries nee to be create as?