SQL Quries

132
Exam 70-433 Preparation: Microsoft SQL Server Database Development Questions 1. James is working for a department store. He has created two tables as follows: 2. CREATE TABLE Inventory.Categories 3. ( 4. CategoryID int identity(1, 1) primary key, 5. Category nvarchar(20) not null 6. ); 7. GO 8. CREATE TABLE Inventory.StoreItems 9. ( 10. ItemNumber nvarchar(10) primary key, 11. CategoryID int foreign key 12. references Inventory.Categories(CategoryID), 13. ItemName nvarchar(60) not null, 14. Size nvarchar(20), 15. UnitPrice money 16. ); 17. GO 18. 19. INSERT INTO Inventory.Categories(Category) 20. VALUES(N'Men'), (N'Women'), (N'Boys'), (N'Girls'),(N'Miscellaneous'); GO The employees of the company are in charge of adding the records to the tables. To keep track of the records added to the StoreItems table, James creates a table as follows: CREATE TABLE Inventory.DatabaseOperations ( OperationID int identity(1,1) NOT NULL, ObjectType nchar(20) default N'Table', ObjectName nvarchar(40), PerformedBy nvarchar(50), ActionPerformed nvarchar(max), TimePerformed datetime, CONSTRAINT PK_Operations PRIMARY KEY(OperationID) ); GO When a new item is added to the StoreItems table, James would like the DatabaseOperations table to r eceive a new record to that eff ect. How can James create a trigger to perform that operation? a. CREATE TRIGGER Inventory.NewProductCreated b. ON Inventory.StoreItems c. AFTER INSERT d. AS e. BEGIN f. INSERT INTO Inventory.DatabaseOperations(ObjectType, g. ObjectName, PerformedBy, h. ActionPerformed, TimePerformed) i. VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(), j. N'New product added', GETDATE())

Transcript of SQL Quries

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 1/132

 

Exam 70-433 Preparation: Microsoft

SQL Server Database Development

Questions1.  James is working for a department store. He has created two tables as follows: 

2.  CREATE TABLE Inventory.Categories3.  (4.  CategoryID int identity(1, 1) primary key,5.  Category nvarchar(20) not null6.  );7.  GO8.  CREATE TABLE Inventory.StoreItems9.  (10.  ItemNumber nvarchar(10) primary key,11.  CategoryID int foreign key

12. 

references Inventory.Categories(CategoryID),13.  ItemName nvarchar(60) not null,14.  Size nvarchar(20),15.  UnitPrice money16.  );17.  GO18. 19.  INSERT INTO Inventory.Categories(Category)20.  VALUES(N'Men'), (N'Women'), (N'Boys'), (N'Girls'),(N'Miscellaneous');

GO

The employees of the company are in charge of adding the records to the tables. Tokeep track of the records added to the StoreItems table, James creates a table as follows: CREATE TABLE Inventory.DatabaseOperations

(OperationID int identity(1,1) NOT NULL,ObjectType nchar(20) default N'Table',ObjectName nvarchar(40),PerformedBy nvarchar(50),ActionPerformed nvarchar(max),TimePerformed datetime,CONSTRAINT PK_Operations PRIMARY KEY(OperationID)

);GO

When a new item is added to the StoreItems table, James would like the DatabaseOperations table to receive a new record to that eff ect. How can James create a trigger to perform that operation?

a.  CREATE TRIGGER Inventory.NewProductCreatedb.  ON Inventory.StoreItemsc.  AFTER INSERTd.  ASe.  BEGINf.  INSERT INTO Inventory.DatabaseOperations(ObjectType,g.  ObjectName, PerformedBy,h.  ActionPerformed, TimePerformed)i.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),j.  N'New product added', GETDATE())

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 2/132

k.  ENDl.  GOm.  CREATE TRIGGER Inventory.NewProductCreatedn.  AFTER INSERTo.  FOR OBJECT::Inventory.StoreItemsp.  ASq.  BEGINr.  INSERT INTO Inventory.DatabaseOperations(ObjectType,s.  ObjectName, PerformedBy,t.  ActionPerformed, TimePerformed)u.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),v.  N'New product added', GETDATE())w.  ENDx.  GOy.  CREATE OBJECT::Inventory.NewProductCreatedz.  ON Inventory.StoreItemsaa.  AS TRIGGER FOR INSERTbb.  BEGINcc.  RETURNdd.  INSERT INTO Inventory.DatabaseOperations(ObjectType,

ee.  ObjectName, PerformedBy,ff.  ActionPerformed, TimePerformed)gg.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),hh.  N'New product added', GETDATE());ii.  ENDjj.  GOkk.  CREATE TRIGGER Inventory.NewProductCreatedll.  ON Inventory.StoreItemsmm.  SET TRIGGER = INSERTnn.  ASoo.  RETURNpp.  INSERT INTO Inventory.DatabaseOperations(ObjectType,qq.  ObjectName, PerformedBy,rr.  ActionPerformed, TimePerformed)ss.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),tt.  N'New product added', GETDATE());uu.  GOvv.  CREATE TRIGGER Inventory.NewProductCreatedww.  ON Inventory.StoreItemsxx.  FOR INSERTyy.  ASzz.  INSERT INTO Inventory.DatabaseOperations(ObjectType,aaa.  ObjectName, PerformedBy,bbb.  ActionPerformed, TimePerformed)ccc.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),ddd.  N'New product added', GETDATE());eee.  GO

21. Dave has a table named Products and that has a f ew records. The table was createdas follows: 

22.  CREATE TABLE Products23.  (24.  ProductID int identity(1, 1) not null,25.  DateAcquired date,26.  Name nvarchar(50),27.  Size nvarchar(32),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 3/132

28.  UnitPrice money,29.  DiscountRate decimal(4, 2),30.  CONSTRAINT PK_Products PRIMARY KEY(ProductID)31.  );

GO

He wants to get a list of products using their names, their sizes, and prices. He 

wants the list to be sorted by the acquired dates but he does not want to see the acquired date column. What code can he use to do that?a.  SELECT DateAcquired, Name, Size, UnitPriceb.  FROM Productsc.  ORDER BY DateAcquiredd.  SET DateAcquired = NULL;e.  GOf.  SELECT DateAcquired, Name, Size, UnitPriceg.  FROM Productsh.  HIDE DateAcquiredi.  ORDER BY DateAcquired;j.  GOk.  SELECT Name, Size, UnitPricel.  FROM Productsm.  ORDER BY DateAcquired;n.  GOo.  SELECT Name, Size, UnitPricep.  ORDER BY DateAcquiredq.  FROM Productsr.  SET DateAcquired SHOW = OFF;s.  GOt.  SELECT DateAcquired, Name, Size, UnitPriceu.  SORT(DateAcquired) IS NULLv.  FROM Products;w.  GO

32. Andrew has been using a table of products that was created as follows: 

33. CREATE TABLE Products34.  (

35.  ProductID int identity(1, 1),36.  Name nvarchar(50),37.  UnitPrice money38.  );

GO

The table already has a f ew records that were added previously. This morning, toadd a f ew records, Andrew writes the following code: INSERT Products(Name, UnitPrice)VALUES(N'Mid Lady Bag - Lizard', 228),

(N'Holly Gladiator Heel Shoes', 198),(N'Short Black Skirt', 55.85);

GO

When he executes that code, Andrew receives the following error: Explicit value must be specified for identity column intable 'Products' either when IDENTITY_INSERT is setto ON or when a replication user is inserting intoa NOT FOR REPLICATION identity column.

What must Andrew do to let the database engine generate a product number?

a.  He must first execute the sp_autogenerate stored procedure 

b.  He must include the list of column in the INSERT statement, as in

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 4/132

c.  INSERT Products(ProductID, Name, UnitPrice)d.  VALUES(N'Mid Lady Bag - Lizard', 228),e.  (N'Holly Gladiator Heel Shoes', 198),f.  (N'Short Black Skirt', 55.85);

GO

g.  She must first use a SELECT statement that calls the MAX(ProductID)

aggregate function to get the highest number that the ProductID columnholds. Then increment it by 1 when adding a new record

h.  He must set IDENTITY _INSERT to OFF 

i.  He must set AUTOINCREMENT to FALSE

39. Steve inherited a table named Customers and that was created as follows: 

40.  CREATE TABLE Customers41.  (42.  AccountNumber nchar(7),43.  FirstName nvarchar(20),44.  LastName nvarchar(20),45.  PhoneNumber nvarchar(20),

46.  EmailAddress nvarchar(40));

Now he wants an index tied to that table. What code can he use to create the index?a.  CREATE INDEX IDX_Customers ON Customers SET IDX_Customers =

AccountNumber;b.  CREATE INDEX IDX_Customers AS AccountNumber FROM Customers;c.  CREATE INDEX IDX_Customers ON Customers(AccountNumber);d.  SELECT AccountNumber FROM Customers CREATE INDEX IDX_Customers;e.  EXEC CREATE INDEX ON Customers(AccountNumber) = IDX_Customers;

47. Ashley receives instructions to create a table of customers with the followingrecommendations: 

  The table will be named Customers 

  The table must have a column that includes a primary key that uses natural numbers starting at 1000 and automacally incrementing by 1

  The table must have a column for the customer's name 

  The table must have a column for the customer's phone number

In what three ways can Ashley create the table?e.  CREATE TABLE Customersf.  (g.  CustomerID INT IDENTITY(1000, 1),h.  Name nvarchar(50),i.  [Phone Number] nvarchar(20),j.  CREATE PRIMARY KEY WITH CustomerID

k.  );l.  GOm.  CREATE TABLE Customersn.  (o.  CustomerID INT IDENTITY(1000, 1)p.  CONSTRAINT PK_Customers PRIMARY KEY(CustomerID),q.  Name nvarchar(50),r.  [Phone Number] nvarchar(20),s.  );t.  GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 5/132

u.  CREATE TABLE Customersv.  (w.  CustomerID INT IDENTITY(1000, 1) PRIMARY KEY,x.  Name nvarchar(50),y.  [Phone Number] nvarchar(20),z.  );aa.  GObb.  CREATE TABLE Customerscc.  (dd.  CustomerID INT IDENTITY(1000, 1),ee.  Name nvarchar(50),ff.  [Phone Number] nvarchar(20),gg.  PRIMARY KEY REFERENCES(CustomerID)hh. ii.  );jj.  GOkk.  CREATE TABLE Customersll.  (mm.  CustomerID INT IDENTITY(1000, 1),nn.  Name nvarchar(50),

oo.  [Phone Number] nvarchar(20),pp.  CONSTRAINT PK_Customers PRIMARY KEY(CustomerID)qq.  );rr.  GO

Hank has the following table of employees: 

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus smallint,HourlySalary money

);GOINSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95),(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GO

Hank also has the following table of sales made by employees during a certainperiod: CREATE TABLE Commercial.Sales(

SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,

SaleDate date,Amount money

);GOINSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 6/132

(N'284-680', N'2011-02-15', 4250),(N'281-730', N'2011-02-15', 3675);

GO

Now, Hank wants to get a list that displays the name of each employee and the number of sales he or she made. What code can he use?

.  SELECT LastName + N', ' + FirstName Employee,a.  COUNT(EmployeeNumber) AS [Number of Sales]b.  FROM Commercial.Sales cs JOINc.  Personnel.Employees pe ONd.  cs.EmployeeNumber = pe.EmployeeNumbere.  GROUP BY LastName + N', ' + FirstNamef.  SELECT LastName + N', ' + FirstName Employee,g.  COUNT(Amount) AS [Number of Sales]h.  FROM Commercial.Sales cs JOINi.  Personnel.Employees pe ONj.  cs.EmployeeNumber = pe.EmployeeNumberk.  GROUP BY LastName + N', ' + FirstNamel.  SELECT LastName + N', ' + FirstName Employee,m.  COUNT(*) AS [Number of Sales]n.  GROUP BY LastName + N', ' + FirstNameo.  FROM Commercial.Sales cs JOINp.  Personnel.Employees pe ONq.  cs.EmployeeNumber = pe.EmployeeNumberr.  SELECT LastName + N', ' + FirstName Employee,s.  COUNT(*) AS [Number of Sales]t.  FROM Commercial.Sales cs JOINu.  Personnel.Employees pe ONv.  cs.EmployeeNumber = pe.EmployeeNumberw.  GROUP BY LastName + N', ' + FirstNamex.  HAVING Amount NOT NULLy.  SELECT LastName + N', ' + FirstName Employee,z.  COUNT(*) AS [Number of Sales]aa.  FROM Commercial.Sales cs JOIN

bb. 

Personnel.Employees pe ONcc.  cs.EmployeeNumber = pe.EmployeeNumberdd.  GROUP BY LastName + N', ' + FirstNameee.  WHERE Amount IS NOT NULL

Mark has a table named Employees created in a schema named Personnel as follows: 

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus tinyint,HourlySalary money

);

GOINSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95);GO

To protect the table, Mark wants to create a view used for data entry. The view will be named NewHire. The view must include a condition that states that the staus of the new employee can be 1, 2, or 3 and no other number. Mark wants to make sure 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 7/132

no record is added to the table if that record violates the condition in the view. Whatcode can he use to accomplish that?

.  CREATE VIEW Personnel.NewHirea.  WITH CHECK OPTIONb.  ASc.  SELECT EmployeeNumber,d.  FirstName,e.  LastName,f.  EmploymentStatus,g.  HourlySalaryh.  FROM Personnel.Employeesi.  WHERE EmploymentStatus IN(1, 2, 3); -- Full Time, Part Time,

Unknown/Otherj.  GOk.  CREATE VIEW Personnel.NewHirel.  ASm.  WITH CHECK OPTIONn.  SELECT EmployeeNumber,o.  FirstName,p.  LastName,

q.  EmploymentStatus,r.  HourlySalarys.  FROM Personnel.Employeest.  WHERE EmploymentStatus IN(1, 2, 3); -- Full Time, Part Time,

Unknown/Otheru.  GOv.  CREATE VIEW Personnel.NewHirew.  SET CHECK OPTION ONx.  ASy.  SELECT EmployeeNumber,z.  FirstName,aa.  LastName,bb.  EmploymentStatus,

cc. 

HourlySalarydd.  FROM Personnel.Employeesee.  WHERE EmploymentStatus IN(1, 2, 3); -- Full Time, Part

Time, Unknown/Otherff.  GOgg.  CREATE VIEW Personnel.NewHirehh.  ASii.  SELECT EmployeeNumber,jj.  FirstName,kk.  LastName,ll.  EmploymentStatus,mm.  HourlySalarynn.  FROM Personnel.Employeesoo.  WHERE EmploymentStatus IN(1, 2, 3) -- Full Time, Part

Time, Unknown/Otherpp.  WITH CHECK OPTION;qq.  GOrr.  CREATE VIEW Personnel.NewHiress.  AStt.  SELECT EmployeeNumber,uu.  FirstName,vv.  LastName,ww.  EmploymentStatus,xx.  HourlySalary

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 8/132

yy.  FROM Personnel.Employeeszz.  CONSTRAINT CHECK EmploymentStatus IN(1, 2, 3); -- Full

Time, Part Time, Unknown/Otheraaa.  GO

What is wrong with the following code?

CREATE TABLE Employees(

EmployeeNumber nchar(60) not null unique primary key,[Full Name] nvarchar(50),HourlySalary money,Department nchar(4)

)

.  Nothing

a.  The nchar data type has a limit length of 12 character but the code indicates 60. Consequently, this code will produce an error

b.  UNIQUE and PRIMARY KEY cannot be combined in the definition of a column

c.  The name of a column cannot have a space 

d.  The code should end with a semi-colon

Imagine you have the following table: 

CREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6),HourlySalary money

);GO

Then you create the following view: CREATE VIEW EmployeesRecordsAS

SELECT EmployeeNumber, FirstName, LastNameFROM Employees;

GO

Now you want to modify the view to add the HourlySalary field. What code wouldyou use to do that?

.  UPDATE VIEW EmployeesRecordsa.  BEGINb.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryc.  FROM Employees;d.  ENDe.  ALTER VIEW EmployeesRecordsf.  ASg.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryh.  FROM Employees;i.  GOj.  EXECUTE sp_change EmployeesRecordsk.  BEGINl.  SELECT EmployeeNumber, FirstName, LastName, HourlySalarym.  FROM Employees;n.  END

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 9/132

o.  ALTER VIEW EmployeesRecordsp.  ASq.  ADD HourlySalaryr.  FROM Employees;s.  ENDt.  ALTER VIEW EmployeesRecordsu.  ASv.  ADD Employees.HourlySalary;w.  GO

Daniel has the following table in his organization: 

CREATE TABLE Members(

MemberID int unique,Name nvarchar(50),MembershipStatus nvarchar(20)

CHECK(MembershipStatus IN (N'In Review', N'Active',N'Suspended'))

);INSERT INTO Members

VALUES(100, N'Albert Welch', N'Active'),(104, N'Daniel Simpson', N'Suspended'),(264, N'Ann Plants', N'Active'),(275, N'Jane Womack', N'In Review'),(279, N'June Palau', N'Suspended'),(288, N'Paul Motto', N'Active');

GO

He wants to remove all members who have been suspended from the organization.What code can he use?

.  DELETE ALL * FROM Membersa.  WHERE MembershipStatus IS N'Suspended';b.  GOc.  DELETE FROM Membersd.

 WHERE MembershipStatus != N'Suspended';e.  GO

f.  REMOVE Membersg.  WHERE MembershipStatus = N'Suspended';h.  GOi.  EXECUTE sp_remove FROM Membersj.  WHERE MembershipStatus = N'Suspended';k.  GOl.  DELETE FROM Membersm.  WHERE MembershipStatus = N'Suspended';n.  GO

Jane has the following tables and their records: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 10/132

  (N'PSNL', N'Personnel'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6)

);GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'RSDV'),

(N'572-384', N'Jeannette', N'Welch', N'PSNL'),(N'279-242', N'Ann', N'Welch', N'HMRS'),(N'495-728', N'Robert', N'Simms', N'RSDV'),(N'382-505', N'Paula', N'Waters', N'PSNL'),(N'268-046', N'Martine', N'Nyamoto', N'ACNT'),(N'773-148', N'James', N'Larsen', N'RSDV'),(N'958-057', N'Peter', N'Aut', N'HMRS');

GOShe wants to change the department of employees from the personnel departmentto the human resources department. When the operation has been performed, she wants to see the list of records that were aff ected. What code can she use?

.  UPDATE Employeesa.  SET DepartmentCode = N'PSNL'b.  OUTPUT INSERTED.*c.  WHERE DepartmentCode = N'HMRS';d.  GOe.  UPDATE Employeesf.  SET DepartmentCode = N'HMRS'g.  OUTPUT INSERTED.*h.  WHERE DepartmentCode = N'PSNL';

i. GOj.  UPDATE Employees

k.  SET DepartmentCode = N'PSNL'l.  WHERE DepartmentCode = N'HMRS'm.  OUTPUT DELETED.*;n.  GOo.  SET Employeesp.  OUTPUT UPDATED.*q.  UPDATE DepartmentCode = N'HMRS'r.  WHERE DepartmentCode = N'PSNL';s.  GOt.  SET Employeesu.  UPDATE DepartmentCode = N'PSNL'v.  WHERE DepartmentCode = N'HMRS'

w.  OUTPUT INSERTED.*;x.  GO

Consider the following table: 

CREATE TABLE StoreItems(

ItemNumber int,ItemName nvarchar(60),ItemDescription nvarchar(max),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 11/132

  UnitPrice money);

GO

What two segment codes can be used to create three records (select 2)?

.  INSERT StoreItemsa.  VALUES(190857, N'Men Deodorant', 5.05);b.  INSERT INTO StoreItemsc.  VALUES(838273, N'Currently on sale bar soaps', N'Sold as 6 bars',

1.95);d.  INSERT StoreItems(ItemNumber, ItemName, ItemDescription)e.  VALUES(828305, N'Cereal in a Jar', N'');f.  INSERT StoreItemsg.  VALUES(149752, N'Chocolate Bar', NULL, 75.00);h.  INSERT INTO StoreItems(ItemNumber, ItemName, UnitPrice)i.  VALUES(350882, N'1Gal Milk 2%', 3.85);j.  INSERT StoreItemsk.  VALUES(247014, N'Peanut Butter Jar', N'Sold in 24 Units', 2.55);l.  INSERT StoreItemsm.  VALUES(197084, N'Tomato Juice', 4.55),n.  (274859, N'Orange Juice', 8.95),o.  (927304, N'2-Litter Soda Bottle', 2.25);p.  INSERT StoreItems(ItemNumber, ItemName, UnitPrice)q.  VALUES(918839, N'Ground Beef', 6.55),r.  (829307, N'Tomato Sauce', 2.95);s.  INSERT StoreItems(ItemNumber, ItemName, ItemDescription)t.  VALUES(941155, N'12-Doughnuts Pack', N'On sale - must go');

You wrote code as follows to give the appropriate permission(s) to Jeremy AndrewBlasick (username: jblasick) to be able to change records on the Employees table: 

CREATE DATABASE NationalBank;GO

USE NationalBank;GOCREATE TABLE Employees(

EmployeeNumber int,FirstName nvarchar(20),LastName nvarchar(20),Salary decimal(20, 6)

);GO

CREATE USER [Jeremy Blasick]

FOR LOGIN jblasick;GO

GRANT UPDATEON OBJECT::EmployeesTO [Jeremy Blasick];GO

INSERT INTO EmployeesVALUES(500727, N'John', N'Harrolds', 18.25);

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 12/132

GO

So you call Jeremy and ask him to change the record in the Employees table. After a f ew minutes, he calls you back saying he can't and he doesn't understand why. Whatdo you think is the problem?

.  In your code, the INSERT INTO code should have been written before the CREATE USER code 

a.  The table doesn't have a primary key and therefore cannot allow recordchange 

b.  The permission(s) given to the user are not enough

c.  You should have revoked the UPDATE permission before granting it because granting the UPDATE permission automatically invokes the INSERT permission

d.  In the CREATE USER statement, you must use either his actual username orhis full name, including his middle name 

You had already created a user as Kirk Blankey. He has been asked to manage the Students table. He must be able to create and change records. He also must be able to give manage the rights of other users on the database. What code can you write to give him the appropriate rights?

.  GRANT INSERT, UPDATE, SELECTa.  TO [Kirk Blankey]b.  ON OBJECT::Students;c.  GOd.  GRANT CONNECT, INSERT, UPDATE,e.  ON OBJECT::Studentsf.  TO User = N'Kirk Blankey'g.  WITH GRANT OPTION;h.  GOi.  GRANT INSERT, UPDATE, SELECTj.  ON OBJECT::Studentsk.  TO [Kirk Blankey]l.  WITH GRANT OPTION;m.  GOn.  GRANT OPTION = ALLo.  TO [Kirk Blankey]p.  ON OBJECT::Students;q.  GOr.  GRANT ALTER, CONNECTs.  ON OBJECT::Studentst.  TO [Kirk Blankey]u.  WITH OPTION GRANT ALL;v.  GO

Consider the following statement from the State University database: 

USE StateUniversity1;GOSELECT TeacherID, FullName, Teachers.CourseIDFROM Teachers;

GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 13/132

What will this statement produce?

.  Nothing, because Teachers.CourseID will create an error

a.  It will show the list of teachers and their TeacherID but excluding the CourseID

b.  It will show the list of teachers, including their TeacherID, their names, andtheir CourseID

c.  An error because it doesn't specify what the primary key of the table is 

d.  It will show the list of teachers that includes only CourseID while excludingthe TeacherID and the full name 

Consider the following table of records 

TeacherID  TeacherName  CourseCode 

218440 Roberta Jerseys CMST 385

250077 Mary Shamberg CMIS 101

270480 Olympia Sumners CMIS 320

274692 Leslie Aronson CMIS 170

294057 Peter Sonnens CMST 306

493748 John Franks CMIS 101

948025 Chryssa Lurie CMIS 320

972837 Hellah Zanogh CMST 306

What statements would produce appropriate results (select 3)?

.  SELECT * FROM Teachersa.  WHERE CourseID = [CMST 320];b.  GOc.  SELECT TeacherID, TeacherName, CourseIDd.  FROM Teachers;e.  GOf.  SELECT * FROM Teachersg.  WHEN CourseID = N'CMIS 101';h.  GOi.  SELECT TeacherName FROM Teachersj.  WHERE TeacherName = N'Leslie Aronson';k.  GOl.  SELECT * FROM Teachersm.  WHERE CourseID = N'CMIS 101';n.  GO

Consider the following statement from the State University database: 

SELECT CourseID, COUNT(*)FROM TeachersGROUP BY CourseID;

GO

What will this statement produce?

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 14/132

.  An error: CourseID should have been written Teachers.CourseID

a.  The list of teachers and the course(s) they teach

b.  The list of teachers, excluding the course(s) they teach

c.  The list of courses and the number of teachers associated with that course 

d.  An error because the statement left out the other columns of the table Consider the following tables: 

Table Name: Courses 

CourseID CourseName  Credits 

CMIS 101Introduction to Problem Solving and Algorithm Design

CMIS 170 Introduction to XML 3 

CMIS 320 Relational Databases  3 

CMIS 420 Advanced Relational Databases  3 

CMST 306 Introduction to Visual Basic Programming 3 

CMST 385 Internet and Web Design 3 

Table Name: Teachers 

TeacherID  FullName 

218440 Roberta Jerseys 

250077 Mary Shamberg

270480 Olympia Sumners 274692 Leslie Aronson

294057 Peter Sonnens 

493748 John Franks 

948025 Chryssa Lurie 

972837 Hellah Zanogh

Table Name: TeachersAndCourses 

CourseID  CourseName  Credits 1 493748 CMIS 101

2  270480 CMIS 328

3  294057 CMST 306

4  274692 CMIS 170

5 274692 CMIS 101

6 972837 CMIS 320

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 15/132

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 16/132

  INSERT INTO Employees(EmployeeNumber, EmplTypeID, FirstName,LastName, HourlySalary)

VALUES(N'202-725', 20, N'Julie', N'Flanell', 36.55),(N'927-205', 10, N'Paulette', N'Simms', 26.65),(N'840-202', 40, N'Alexandra', N'Ulm', 12.85),(N'472-095', 10, N'Ellie', N'Tchenko', 11.95),(N'268-046', 60, N'Martine', N'Nyamoto', 15.52),(N'273-148', 20, N'James', N'Larsen', 18.24),(N'481-729', 10, N'Faye', N'Cross', 14.92);

GO

What will the following code produce?SELECT EmplTypeID FROM EmployeesWHERE HourlySalary < ALL(SELECT AVG(HourlySalary)

FROM EmployeesGROUP BY EmplTypeID);

GO

.  The employment type ID of the staff member who has the highest salary

a.  The names of all staff members who have the highest salaries peremployment type 

b.  The name of the staff member who has the highest salary in each type of employment

c.  The highest salary in each type of employment

d.  The salary of the staff member with the highest hourly salary

Your boss created a database named SuperMarket1 for a new client. He wants you tocreate a new table for the employees and to let Hermine Nkolo (Login Name: hnkolo)manage that table. To make it happen, you write the following script: 

CREATE SCHEMA Personnel;GOCREATE TABLE Personnel.Employees

(EmplNbr nchar(10),FirstName nvarchar(20),LastName nvarchar(20),Salary money,FullTime bit

);GOINSERT INTO Personnel.EmployeesVALUES(N'29730', N'Philippe', N'Addy', 20.05, 1),

(N'28084', N'Joan', N'Shepherd', 12.72, 0),(N'79272', N'Joshua', N'Anderson', 18.26, 0),(N'22803', N'Gregory', N'Swanson', 15.95, 0),(N'83084', N'Josephine', N'Anderson', 22.25, 1);

GO

CREATE USER HermineFOR LOGIN hnkolo;GOGRANT INSERTON OBJECT::Personnel.EmployeesTO Hermine;GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 17/132

  GRANT UPDATEON OBJECT::Personnel.EmployeesTO Hermine;

GO

After executing the script, you let Hermine know that she can use the table. After a f ew minutes, Hermine calls you and says that when she tries to create records, she 

receives an error that she was denied the ob ject: 

How would you solve the problem: 

.  It appears that Hermine doesn't have the CONNECT permission. Therefore, grant the CONNECT permission to Hermine 

a.  In the SQL Server Management Studio, open the properties of the Employees table and change the User name from Hermine to hrnkolo

b.  Grant the SELECT permission

c.  Your account probably does not have the WITH GRANT permission

d.  The table was marked as read-only

Pamela McDugan wrote the following statement to create a view: 

CREATE VIEW Personnel.ContractorsASSELECT empl.EmployeeNumber, empl.[First Name], empl.[Last Name]FROM Personnel.Employees emplORDER BY empl.[Last Name]

GO

When she executes the statement to create the view, she receives an error. What is wrong?

.  You cannot include an ORDER BY clause in this view

a.  The error is because empl is not in square brackets but the names of columns are 

b.  The names of columns must not contain space 

c.  The ORDER BY clause must be written before the FROM expression

d.  The name of the view must not be preceded by Personnel. In can only be preceded by the name of the database 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 18/132

  Michael wants to test something on a view. So he writes the following code to create a temporary table and a view: 

CREATE TABLE #Employees(

EmployeeNumber nchar(10) not null,[First Name] nvarchar(20),

[Last Name] nvarchar(20) not null,Title nvarchar(40),Salary money

);GO

CREATE VIEW ContractorsASSELECT EmployeeNumber, [First Name], [Last Name]FROM #Employees;

GO

When he executes the code, she receives an error. What do you think is the problem?

.  You cannot create a view for a temporary table 

a.  The name of the view must be preceded by #

b.  The names of columns must not contain space 

c.  The name of the table in the view definition must not start with #

d.  In order to create a view from a temporary table, the table must have a primary key

Consider the tables from the State University database. Imagine you want to get a list of all teachers whether they have a matching CourseID in the Courses table or not.What statement would produce that result?

. SELECT TeacherID, FullName, Teachers.CourseIDa.  FROM Teachers

b.  LEFT OUTER JOIN Coursesc.  ON Teachers.CourseID = Courses.CourseID;d.  GOe.  SELECT TeacherID, FullName, Teachers.CourseIDf.  FROM Teachersg.  CROSS OUTER JOIN Coursesh.  ON Teachers.CourseID = Courses.CourseID;i.  GOj.  SELECT TeacherID, FullName, Teachers.CourseIDk.  FROM Teachersl.  SELECT Courses.CourseID, Courses.CourseNamem.  FROM Coursesn.  LEFT OUTER JOIN Teacherso.  ON Teachers.CourseID = Courses.CourseID;p.  GOq.  SELECT TeacherID, FullName, Teachers.CourseIDr.  FROM Teachers CROSS JOIN Courses

Consider the following tables: 

CREATE TABLE Departments(

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 19/132

  DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

CREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6)

);GO

What would the following statement produce?

SELECT Employees.EmployeeNumber,Employees.FirstName,Employees.LastName,Departments.DepartmentName

FROM Employees INNER JOIN DepartmentsON Departments.DepartmentCode = Employees.DepartmentCode;

GO

.  The list of all employees and the department each employee belongs to

a.  The list of each employee associated with each department (not just the department the employee actually belongs to)

b.  Nothing, because the Employees table does not specify a foreign key

c.  An error, because the SELECT statement should start with the Departments table 

d.  The list of departments and all employees 

Consider the following tables: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

CREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),

LastName nvarchar(20),DepartmentCode nchar(6));

GO

What is the diff erence in result between this code: 

SELECT Employees.EmployeeNumber,Employees.FirstName,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 20/132

Employees.LastName,Departments.DepartmentName

FROM Employees INNER JOIN DepartmentsON Departments.DepartmentCode = Employees.DepartmentCode;

GO

and this one?

SELECT Employees.EmployeeNumber,Employees.FirstName,Employees.LastName,Departments.DepartmentName

FROM Employees JOIN DepartmentsON Departments.DepartmentCode = Employees.DepartmentCode;

GO

.  The first code would show the list of each employee associated with eachdepartment. The second code would produce an error

a.  No diff erence: both would produce the same result

b.  The first code would produce an error. The second code would show eachdepartment associated with each employee 

c.  Both codes would produce errors because the assignment is ill-formed

d.  The first code will show the list of each employee and the department he orshe belongs to.The second code will show each department with each employee of the company, even if the employee doesn't belong to the department

Consider the following tables: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),

LastName nvarchar(20),DepartmentCode nchar(6)

);GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'RSDV'),

(N'495-728', N'Robert', N'Simms', N'RSDV'),(N'268-046', N'Martine', N'Nyamoto', N'ACNT'),(N'273-148', N'James', N'Larsen', N'RSDV'),(N'958-057', N'Peter', N'Aut', N'HMRS');

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 21/132

GO

What would the following code produce?SELECT Employees.EmployeeNumber,

Employees.FirstName,Employees.LastName,Departments.DepartmentName

FROM Personnel.Employees JOIN DepartmentsON Departments.DepartmentCode = Employees.DepartmentCode

WHERE Departments.DepartmentCode = N'RSDV';GO

.  An error, because the WHERE clause must be written before the FROM clause 

a.  An error because the Departments.DepartmentCode field is not specified inthe SELECT statement

b.  A list of employees who belong to the Research & Development department

c.  A list of departments that are joined to the employees 

d.  A list that contains each department and all employees, start with those thatbelong to the department that has RSDV as code 

Maggie is in charge of managing an old table from an existing database. Now she wants to delete the table but is worried about the consequences and calls you to get yourinput. What type of answer would you present her?

.  You can't delete a view once it has been created and executed at least once 

a.  Delete the table or tables that hold(s) the records of the view

b.  Delete the schema first

c.  Check the records first and copy them to a temporary table 

d.  Check the permissions first and find out how they are used on the view

Gary wants to monitor the transactions in a bank database. So he wrote code as 

follows: 

CREATE SCHEMA Management;GOCREATE SCHEMA Personnel;GOCREATE SCHEMA Transactions;GO

GOCREATE TABLE Transactions.Customers(

CustomerID int Identity(1, 1) NOT NULL,DateCreated date,AccountNumber nvarchar(20),

CustomerName nvarchar(50));GOCREATE TABLE Transactions.DatabaseOperations(

OperationID int identity(1,1) NOT NULL,ObjectName nvarchar(40),PerformedBy nvarchar(50),ActionPerformed nvarchar(50),CONSTRAINT PK_DBOperations PRIMARY KEY(OperationID)

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 22/132

  );GO

CREATE TRIGGER Personnel.ForCustomersON Transactions.CustomersAFTER INSERTASBEGIN

INSERT INTO Management.DatabaseOperations(ObjectName,PerformedBy, ActionPerformed)

VALUES(N'Customers', SUSER_SNAME(), N'Processed a deposit')END

GO

Frank executes that code and it works fine.To test the database, Gary writes the following code to add a new record to the Customers table: INSERT INTO Transactions.Customers(DateCreated, AccountNumber,CustomerName)VALUES(N'02/20/2011', N'000-2860-3982', N'John Doe');GO

When he executes it, Gary receives an error. What do you think is the problem?

.  Triggers don't need a schema 

a.  Since neither a user nor a login was created in the code, it appears that Frankdoesn't have the right permissions to the trigger

b.  In the code for the trigger, Frank must specify the schema of the Customers table as Management (as in Management.Customers)

c.  Both the Customers table and the ForCustomers triger must use the same schema 

d.  The DatabaseOperations table called in the trigger must not have a primarykey constraint

Martha inherited the following table and its records 

CREATE SCHEMA Listing;GOCREATE TABLE Listing.Apartments(

UnitNumber int not null,Bedrooms int,Bathrooms real,Price money,Deposit money,Available bit

);

GOINSERT Listing.ApartmentsVALUES(104, 2, 1.00, 1050.00, 300.00, 0),

(306, 3, 2.00, 1350.00, 425.00, 1),(105, 1, 1.00, 885.00, 250.00, 1),(202, 1, 1.00, 950.00, 325.00, 0),(304, 2, 2.00, 1250.00, 300.00, 0),(106, 3, 2.00, 1350.00, 425.00, 1),(308, 0, 1.00, 875.00, 225.00, 1),(203, 1, 1.00, 885.00, 250.00, 1),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 23/132

  (204, 2, 2.00, 1125.00, 425.00, 1),(205, 1, 1.00, 1055.00, 350.00, 0);

GO

The documentation of the database indicates that the first symbol of a unit numberindicates the f loor level. What two codes would let Martha see the list of apartments from the second f loor?

. SELECT *a.  FROM Units

b.  WHERE UnitNumber IN N'200' UP;c.  GOd.  SELECT ALL *e.  FROM Unitsf.  WHERE UnitNumber LIKE N'2%';g.  GOh.  SELECT ALL *i.  FROM Unitsj.  WHERE UnitNumber FROM 200 TO 299;k.  GOl.  SELECT ALL *m.  FROM Unitsn.  WHERE UnitNumber BETWEEN N'200' AND N'300';o.  GOp.  SELECT ALL *q.  FROM UnitNumberr.  FROM 200 TO 300;s.  GO

Ann has been asked to create a table of employees. The table must contain a columnfor hourly salaries and, during data entry, the table must re ject any salary lower than12.50. What code can she use to take care of this (Select Two)?

.  CREATE TABLE Employeesa.  (b.  EmployeeNumber nchar(60) not null,

c.  [Full Name] nvarchar(50),d.  HourlySalary money CHECK(HourlySalary !< 12.50),e.  Department nchar(4)f.  );g.  GOh.  CREATE TABLE Employeesi.  (j.  EmployeeNumber nchar(60) not null,k.  [Full Name] nvarchar(50),l.  HourlySalary money CONSTRAINT CHECK(HourlySalary >= 12.50),m.  Department nchar(4)n.  );o.  GOp.  CREATE TABLE Employeesq.  (r.  EmployeeNumber nchar(60) not null,s.  [Full Name] nvarchar(50),t.  HourlySalary money,u.  Department nchar(4),v.  CONSTRAINT CK_HourlySalary CHECK (HourlySalary >= 12.50)w.  );x.  GOy.  CREATE TABLE Employees

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 24/132

z.  (aa.  EmployeeNumber nchar(60) not null,bb.  [Full Name] nvarchar(50),cc.  HourlySalary money,dd.  Department nchar(4),ee.  CONSTRAINT CK_HourlySalary CHECK ON (HourlySalary >=

12.50)ff.  );gg.  GOhh.  CREATE TABLE Employeesii.  (jj.  EmployeeNumber nchar(60) not null,kk.  [Full Name] nvarchar(50),ll.  HourlySalary money,mm.  Department nchar(4),nn.  CONSTRAINT ON HourlySalary FOR CHECK(HourlySalary >=

12.50)oo.  );pp.  GO

Annouar has the following tables and their records: 

CREATE TABLE Departments(

Code nchar(4),Name nvarchar(40)

);GOCREATE TABLE Employees(

EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),HourlySalary money,Department nchar(4)

);GO

INSERT DepartmentsVALUES(N'HMNR', N'Human Resources'), (N'ACNT', N'Accounting'),

(N'RNDV', N'Research & Development');GOINSERT EmployeesVALUES(N'60-224', N'Frank Roberts', 20.15, N'RNDV'),

(N'29-742', N'Marcial Engolo', 12.58, N'HMNR'),(N'82-073', N'John Duchant', 22.75, N'RNDV'),(N'82-503', N'Marthe Mengue', 15.86, N'ACNT'),(N'47-582', N'Hervey Arndt', 12.24, N'HMNR'),(N'82-263', N'Mark Edwards', 18.14, N'RNDV');

GOHe is using the following code to get some statistics about the employees: SELECT MAX(HourlySalary)FROM Employees emplsWHERE empls.Department = N'RNDV';GO

What will that code produce?

.  20.15

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 25/132

a.  12.58

b.  22.75

c.  15.86

d.  18.14 

Benjamin has the followng tables and their records: CREATE TABLE Departments(

Code nchar(4),Name nvarchar(40)

);GOCREATE TABLE Employees(

EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),Department nchar(4)

);

GO

INSERT DepartmentsVALUES(N'HMNR', N'Human Resources'), (N'ACNT', N'Accounting'),

(N'RNDV', N'Research & Development');GOINSERT EmployeesVALUES(N'60-224', N'Frank Roberts', N'RNDV'),

(N'29-742', N'Marcial Engolo', N'HMNR'),(N'82-073', N'John Duchant', N'RNDV'),(N'82-503', N'Marthe Mengue', N'ACNT'),(N'47-582', N'Hervey Arndt', N'HMNR'),(N'82-263', N'Mark Edwards', N'RNDV');

GO

He wants to know the number of employees in the human resources department.Which one of the following codes can he use?

.  SELECT ALL COUNT(*) AS [# of Employees] FROM Employees emplsa.  WHERE empls.Department = N'HMNR';b.  GOc.  SELECT ALL COUNT(*) FROM Employees empls AS [# of Employees]d.  WHERE empls.Department = N'HMNR';e.  GOf.  SELECT FROM Employees empls ALL COUNT(*) AS [# of Employees]g.  WHERE empls.Department = N'HMNR';h.  GOi.  SELECT COUNT(*) AS [# of Employees]j.  WHERE empls.Department = N'HMNR'k.  FROM Employees empls;l.  GOm.  SELECT WHERE empls.Department = N'HMNR'n.  COUNT(*) AS [# of Employees] FROM Employees empls;o.  GO

Peter has the following table and its records: 

CREATE TABLE Employees(

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 26/132

  EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),HourlySalary money

);GO

INSERT EmployeesVALUES(N'60-224', N'Frank Roberts', 12.48),

(N'29-742', N'Marcial Engolo', 30.15),(N'82-073', N'John Duchant', 8.75),(N'82-503', N'Marthe Mengue', 14.85),(N'47-582', N'Hervey Arndt', 10.06),(N'82-263', N'Mark Edwards', 25.55);

GO

He wants to get the highest salary. What code can he use?.  SELECT FROM Employees empls MAX(HourlySalary) AS [HighestSalary];

a.  GOb.  SELECT FROM Employees empls HIGH(HourlySalary) AS [Highest

Salary];

c.  GOd.  SELECT MAX(HourlySalary) AS [Highest Salary] FROM Employeesempls;

e.  GOf.  SELECT HIGH(HourlySalary) AS [Highest Salary] FROM Employees

empls;g.  GOh.  SELECT MAX(HourlySalary) FROM Employees empls AS [Highest

Salary];i.  GO

Frank has various employees in diff erent departments created in the followingDepartments and Employees tables: 

CREATE TABLE Departments(Code nchar(4),Name nvarchar(40)

);GOCREATE TABLE Employees(

EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),HourlySalary money,Department nchar(4)

);GO

INSERT DepartmentsVALUES(N'HMNR', N'Human Resources'), (N'ACNT', N'Accounting'),

(N'RNDV', N'Research & Development');GOINSERT EmployeesVALUES(N'60-224', N'Frank Roberts', 20.15, N'RNDV'),

(N'29-742', N'Marcial Engolo', 12.58, N'HMNR'),(N'82-073', N'John Duchant', 22.75, N'RNDV'),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 27/132

  (N'66-080', N'Mark Roberts', 10.12, N'HMNR'),(N'82-503', N'Marthe Mengue', 15.86, N'ACNT'),(N'47-582', N'Hervey Arndt', 12.24, N'HMNR'),(N'82-263', N'Mark Edwards', 18.14, N'RNDV');

GO

He wants to find the highest salary in the human resources department. What code 

can he use to get it?.  SELECT MAX(HourlySalary)a.  FROM Employees empl INNER JOIN Departments deptb.  ON empl.Department = dept.Codec.  WHERE dept.Code = N'RNDV';d.  GOe.  SELECT MAX(HourlySalary)f.  FROM Employees empl INNER JOIN Departments deptg.  ON empl.Department = dept.Codeh.  WHERE dept.Code = N'HMNR';i.  GOj.  SELECT MAX(HourlySalary)k.  FROM Employees empl CROSS OUTER JOIN Departments deptl.  ON empl.Department = dept.Codem.  WHERE dept.Code = N'HMNR';n.  GOo.  SELECT HIGH(HourlySalary)p.  FROM Employees empl INNER JOIN Departments deptq.  ON empl.Department = dept.Coder.  WHERE NOT dept.Code = N'RNDV';s.  GOt.  SELECT sp_high(HourlySalary)u.  FROM Employees empl JOIN Departments deptv.  ON empl.Department = dept.Codew.  WHERE dept.Code = N'HMNR';x.  GO

Lynda has the following table and its records: 

CREATE TABLE Employees(

EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),HourlySalary money,Manager nchar(6)

);GOINSERT EmployeesVALUES(N'60-224', N'Frank Roberts', 20.15, NULL),

(N'29-742', N'Marcial Engolo', 12.58, NULL),(N'82-073', N'John Duchant', 22.74, N'29-742'),(N'82-503', N'Marthe Mengue', 15.86, N'60-224'),

(N'44-440', N'Paul Motto', 16.22, N'29-742'),(N'47-582', N'Hervey Arndt', 12.24, N'60-224'),(N'82-263', N'Mark Edwards', 18.14, N'29-742');

GO

She wants to get the list of employees with the last column showing each employee's manager name. What code would produce that list?

.  SELECT empl.EmployeeNumber AS [Empl #],a.  empl.[Full Name],b.  empl.HourlySalary AS Salary,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 28/132

c.  (SELECT mgr.[Full Name]d.  FROM Employees mgre.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managerf.  FROM Employees empl;g.  GOh.  SELECT empl.EmployeeNumber AS [Empl #],i.  empl.[Full Name],j.  empl.HourlySalary AS Salary,k.  (SELECT mgr.EmployeeNumber, mgr.[Full Name]l.  FROM Employees mgrm.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managern.  FROM Employees empl;o.  GOp.  SELECT empl.EmployeeNumber AS [Empl #],q.  empl.[Full Name],r.  empl.HourlySalary AS Salary,s.  (SELECT mgr.[Full Name]t.  FROM Employees mgru.  WHERE empl.Manager = mgr.Manager) AS Managerv.  FROM Employees empl;w.  GOx.  SELECT empl.EmployeeNumber AS [Empl #],y.  empl.[Full Name],z.  empl.HourlySalary AS Salary,aa.  (SELECT mgr.EmployeeNumberbb.  FROM Employees mgrcc.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managerdd.  FROM Employees empl;ee.  GOff.  SELECT empl.EmployeeNumber AS [Empl #],gg.  empl.[Full Name],hh.  empl.HourlySalary AS Salary,ii.  (SELECT mgr.[Full Name]jj.  FROM Employees mgrkk.  WHERE empl.[Full Name] = mgr.[Full Name]) AS Managerll.  FROM Employees empl;mm.  GO

Janice has the following table and its records: 

CREATE TABLE Employees(

EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),HourlySalary money,Manager nchar(6)

);GO

INSERT EmployeesVALUES(N'60-224', N'Frank Roberts', 20.15, NULL),

(N'29-742', N'Marcial Engolo', 12.58, NULL),(N'82-073', N'John Duchant', 22.74, N'29-742'),(N'82-503', N'Marthe Mengue', 15.86, N'60-224'),(N'44-440', N'Paul Motto', 16.22, N'29-742'),(N'47-582', N'Hervey Arndt', 12.24, N'60-224'),(N'82-263', N'Mark Edwards', 18.14, N'29-742');

GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 29/132

She wants to get the list of employees with the last column showing each employee's manager name but the list must include only the employees who have a manager.What code would produce that list?

.  SELECT empl.EmployeeNumber AS [Empl #],a.  empl.[Full Name],b.  empl.HourlySalary AS Salary,

c. 

(SELECT mgr.[Full Name]d.  FROM Employees mgre.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managerf.  FROM Employees emplg.  WHERE empl.Manager IS NOT NULL;h.  GOi.  SELECT empl.EmployeeNumber AS [Empl #],j.  empl.[Full Name],k.  empl.HourlySalary AS Salary,l.  (SELECT mgr.[Full Name]m.  FROM Employees mgrn.  WHERE (empl.Manager = mgr.EmployeeNumber)o.  AND empl.Manager IS NOT NULL) AS Managerp.  FROM Employees empl;q.  GOr.  SELECT empl.EmployeeNumber AS [Empl #],s.  empl.[Full Name],t.  empl.HourlySalary AS Salary,u.  (SELECT mgr.[Full Name]v.  FROM Employees mgrw.  WHERE empl.Manager = mgr.Manager) AS Manager IS NOT NULLx.  FROM Employees empl;y.  GOz.  SELECT empl.EmployeeNumber AS [Empl #],aa.  empl.[Full Name],bb.  empl.HourlySalary AS Salary,cc.  (SELECT mgr.EmployeeNumber

dd. 

FROM Employees mgree.  WHERE (empl.Manager = mgr.EmployeeNumber)ff.  AND empl.Manager IS NOT NULL) AS Managergg.  FROM Employees empl;hh.  GOii.  SELECT empl.EmployeeNumber AS [Empl #],jj.  empl.[Full Name],kk.  empl.HourlySalary AS Salary,ll.  (SELECT mgr.[Full Name] NOT NULLmm.  FROM Employees mgrnn.  WHERE empl.[Full Name] = mgr.[Full Name]) AS Manageroo.  FROM Employees empl;pp.  GO

Ann created the following table of employees and filled it up with the necessaryrecords as follows: 

CREATE TABLE Employees(

EmployeeNumber nchar(6) not null unique,[Full Name] nvarchar(50),Manager nchar(6)

);GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 30/132

 INSERT EmployeesVALUES(N'60-224', N'Frank Roberts', NULL),

(N'29-742', N'Marcial Engolo', NULL),(N'82-073', N'John Duchant', N'29-742'),(N'82-503', N'Marthe Mengue', N'60-224'),(N'44-440', N'Paul Motto', N'29-742'),(N'47-582', N'Hervey Arndt', N'60-224'),(N'82-263', N'Mark Edwards', N'29-742');

GO

To keep employee's personal information private, she created an additional table that contains employees salaries as follows: CREATE TABLE Salaries(

SalaryID int identity(1, 1) primary key,EmployeeNumber nchar(6) not null,HourlySalary money not null,

);GO

INSERT Salaries(EmployeeNumber, HourlySalary)VALUES(N'60-224', 20.15),

(N'29-742', 12.58),(N'82-073', 22.74),(N'82-503', 15.86),(N'44-440', 16.22),(N'47-582', 12.24),(N'82-263', 18.14);

GO

Now, Ann wants to see the list of employees so that each record shows anemployee's salary and his or her manager name. What code would produce thatresult?

.  SELECT empl.EmployeeNumber AS [Empl #],a.

 empl.[Full Name],b.  (SELECT sal.SalaryID, sal.HourlySalary

c.  FROM Salaries sald.  WHERE empl.EmployeeNumber = sal.EmployeeNumber) AS

Salary,e.  (SELECT mgr.EmployeeNumber, mgr.[Full Name]f.  FROM Employees mgrg.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managerh.  FROM Employees empl;i.  GOj.  SELECT empl.EmployeeNumber AS [Empl #],k.  empl.[Full Name],l.  (SELECT sal.HourlySalarym.  FROM Salaries sal

n.  WHERE empl.EmployeeNumber = sal.EmployeeNumber) ASSalary,

o.  (SELECT mgr.[Full Name]p.  FROM Employees mgrq.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managerr.  FROM Employees empl;s.  GOt.  SELECT empl.EmployeeNumber AS [Empl #],u.  empl.[Full Name],

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 31/132

v.  (SELECT sal.HourlySalaryw.  FROM Salaries salx.  WHERE empl.EmployeeNumber = sal.SalaryID) AS Salary,y.  (SELECT mgr.[Full Name]z.  FROM Employees mgraa.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managerbb.  FROM Employees empl;cc.  GOdd.  SELECT empl.EmployeeNumber AS [Empl #],ee.  empl.[Full Name],ff.  (SELECT sal.EmployeeNumbergg.  FROM Salaries salhh.  WHERE empl.EmployeeNumber = sal.EmployeeNumber) AS

Salary,ii.  (SELECT mgr.EmployeeNumberjj.  FROM Employees mgrkk.  WHERE empl.SalaryID = mgr.SalaryID) AS Managerll.  FROM Employees empl;mm.  GOnn.  SELECT empl.EmployeeNumber AS [Empl #],

oo.  empl.[Full Name],pp.  (SELECT sal.EmployeeNumberqq.  FROM Salaries salrr.  WHERE empl.EmployeeNumber = sal.EmployeeNumber) AS

Salary,ss.  (SELECT mgr.EmployeeNumbertt.  FROM Employees mgruu.  WHERE empl.Manager = mgr.EmployeeNumber) AS Managervv.  FROM Employees empl;ww.  GO

To monitor the base salaries of employees in his company, John's has a table namedStartingSalaries as follows: 

CREATE SCHEMA Personnel;GOCREATE TABLE Personnel.StartingSalaries(

Category nvarchar(30) not null,StartingSalary money null

);INSERT INTO Personnel.StartingSalariesVALUES(N'Base', 10.00),

(N'Intern', 12.35),(N'Regular', 14.50),(N'Manager', 20.00);

GO

John also has a table of employees as follows: 

CREATE TABLE Personnel.Employees(

EmployeeID int identity(1,1) NOT NULL,EmployeeNumber nchar(10),FirstName nvarchar(32),LastName nvarchar(32) NOT NULL,Title nvarchar(50),HourlySalary money,CONSTRAINT PK_Employees PRIMARY KEY (EmployeeID)

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 32/132

);GOINSERT INTO Personnel.Employees(EmployeeNumber, FirstName,

LastName, Title, HourlySalary)VALUES(N'662-286', N'Lienev', N'Zbrnitz', N'Cashier', 15.75),

(N'487-525', N'Paulin', N'Guerrero', N'Intern', 10.85),(N'395-138', N'Plant', N'Waste', N'Head Teller', 16.75),(N'822-730', N'Steven', N'Chang', N'Intern', 14.15),(N'930-717', N'Abedi', N'Kombo', N'Shift Leader', 10.02),(N'573-048', N'Paul', N'Landsford', N'Cashier', 12.25);

GO

Now, John wants to get a list of employees whose salary is lower than the intern's salary of the StartingSalaries table. What code can John use: 

.  SELECT empl.*a.  FROM Personnel.Employees emplb.  WHERE empl.Title <= (SELECT Categoryc.  FROM Personnel.StartingSalaries sal);d.  GOe.  SELECT empl.*f.  FROM Personnel.Employees emplg.  WHERE empl.HourlySalary <= (SELECT sal.Categoryh.  FROM Personnel.StartingSalaries sali.  WHERE sal.StartingSalary = 12.35);j.  GOk.  SELECT empl.*l.  FROM Personnel.Employees emplm.  WHERE empl.HourlySalary <= (SELECT sal.StartingSalaryn.  FROM Personnel.StartingSalaries salo.  WHERE sal.Category = N'Intern');p.  GOq.  SELECT empl.EmployeeNumber,r.  empl.FirstName,s.  empl.LastName,

t. 

empl.Title,u.  (SELECT sal.StartingSalaryv.  FROM Personnel.StartingSalaries salw.  WHERE sal.Category = N'Intern')x.  FROM Personnel.Employees empl;y.  GOz.  SELECT empl.EmployeeNumber,aa.  empl.FirstName,bb.  empl.LastName,cc.  empl.Title,dd.  (SELECT sal.Categoryee.  FROM Personnel.StartingSalaries salff.  WHERE sal.StartingSalary = 12.3500)gg.  FROM Personnel.Employees empl;

hh.  GO

Eric has the following tables and their records: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 33/132

 INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),(N'PSNL', N'Personnel'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6)

);GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'RSDV'),

(N'572-384', N'Jeannette', N'Welch', N'PSNL'),(N'279-242', N'Ann', N'Welch', N'HMRS'),(N'495-728', N'Robert', N'Simms', N'RSDV'),

(N'382-505', N'Paula', N'Waters', N'PSNL'),(N'268-046', N'Martine', N'Nyamoto', N'ACNT'),(N'773-148', N'James', N'Larsen', N'RSDV'),(N'958-057', N'Peter', N'Aut', N'HMRS');

GO

He wants to move all employees from the personnel department to the humanresources department. Which of the following codes would produce the right result?

.  UPDATE Employeesa.  SET DepartmentCode = N'PSNL'b.  WHERE DepartmentCode = N'HMRS';c.  GOd.  UPDATE Employeese.  SET DepartmentCode = N'HMRS'

f. WHERE DepartmentCode = N'PSNL';g.  GO

h.  UPDATE FROM Employeesi.  SET DepartmentCode = N'PSNL'j.  WHERE DepartmentCode = N'HMRS';k.  GOl.  SET Employeesm.  UPDATE DepartmentCode = N'HMRS'n.  WHERE DepartmentCode = N'PSNL';o.  GOp.  SET Employeesq.  UPDATE DepartmentCode = N'PSNL'r.  WHERE DepartmentCode = N'HMRS';s.  GO

You have a table named ObsoleteProducts and you are asked to remove all of its records. What code would you use to do it?

.  DROP ObsoleteProducts;a.  REMOVE ALL * FROM ObsoleteProducts;b.  DELETE * FROM ObsoleteProducts;c.  DELETE ObsoleteProducts;d.  EXECUTE sp_removerecords FROM ObsoleteProducts;

Consider the following table of employees: 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 34/132

  CREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6)

);

Imagine you want to provide your users with a view that shows the employees numbers and their name. What code can you use to create such a view?

.  CREATE VIEW EmployeesRecordsa.  BEGINb.  SELECT empl.EmployeeNumber, empl.FirstName, empl.LastNamec.  FROM Employees empl;d.  ENDe.  CREATE VIEW EmployeesRecordsf.  ASg.  SELECT empl.EmployeeNumber, empl.FirstName, empl.LastNameh.  FROM Employees empl;i.  CREATE VIEW EmployeesRecordsj.  RETURN TABLEk.  ASl.  SELECT empl.EmployeeNumber, empl.FirstName, empl.LastNamem.  FROM Employees empl;n.  CREATE VIEW EmployeesRecords1o.  WITH SELECT empl.EmployeeNumber, empl.FirstName, empl.LastNamep.  FROM Employees empl;q.  ENDr.  EXECUTE CREATE EmployeesRecords1 AS VIEWs.  SELECT empl.EmployeeNumber, empl.FirstName, empl.LastNamet.  FROM Employees empl;

Imagine you have the following table: 

CREATE TABLE Employees

( EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6)

);GO

Now you want to change the table to add a column named HourlySalary of type money. What code would you use to do that?

.  UPDATE TABLE Employeesa.  BEGINb.  ADD HourlySalary money;c.  ENDd.  ALTER TABLE Employees ADD HourlySalary money;e.  EXECUTE MODIFY TABLE Employeesf.  ASg.  ADD HourlySalary money;h.  ENDi.  EXECUTE sp_change TABLE Employeesj.  ASk.  BEGINl.  ADD HourlySalary money;m.  END

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 35/132

n.  ALTER TABLE Employeeso.  ASp.  BEGINq.  ADD COLUMN HourlySalary money;r.  END

You inherited a table that was created as follows: 

CREATE TABLE Employees(

FirstName nvarchar(20),LastName nvarchar(20),DepartmentCode nchar(6)

);GO

Now you want to add a primary key column named EmployeeNumber of type int.What two codes can you use to perform that operation?

.  WITH Employeesa.  SET EmployeeNumber int not null PRIMARY KEY(EmployeeNumber);b.  GOc.  ALTER TABLE Employeesd.  ADD EmployeeNumber int not null PRIMARY KEY;e.  GOf.  ALTER TABLE Employeesg.  ADD EmployeeNumber int not nullh.  CONSTRAINT PK_Employees PRIMARY KEY(EmployeeNumber);i.  GOj.  UPDATE TABLE Employeesk.  ADD COLUMN EmployeeNumber int not null PRIMARY

KEY(EmployeeNumber);l.  GOm.  ALTER TABLE Employeesn.  ADD EmployeeNumber int not nullo.  ALTER EmployeeNumber AS PRIMARY KEY(EmployeeNumber);

p. GO

Mark has a table of employees and it contains a column named LastName. Markwants to get a list of employees whose last name ends with the letter a. What code can he use?

.  SELECT ALL * FROM Employeesa.  WHERE LastName LIKE N'%a';b.  GOc.  SELECT ALL * FROM Employeesd.  WHERE LastName LIKE N'_a';e.  GOf.  SELECT ALL * FROM Employeesg.  WHERE LastName LIKE N'[a]';h.  GOi.  SELECT * FROM Employeesj.  WHERE LastName LIKE N'[^a]';k.  GOl.  SELECT ALL * FROM Employeesm.  WHERE LastName LIKE N'!a';n.  GO

Alex has the following table named Products: 

CREATE TABLE Products

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 36/132

  (ItemNumber int primary key,Name nvarchar(50),UnitPrice money,Discount decimal(4, 2)

);

Many records have been added to the table. Some records have a value for the discount and some do not. To get a list of products that do not have a discount, whatcode can Alex use?

.  SELECT ItemNumber, Name, UnitPrice, Discounta.  FROM Productsb.  WHERE Discount = 0;c.  GOd.  SELECT ItemNumber, Name, UnitPrice, Discounte.  FROM Productsf.  WHERE Discount = NULL;g.  GOh.  SELECT *i.  FROM Productsj.  WHERE Discount <> 0;k.  SELECT *l.  FROM Productsm.  WHERE Discount NULL;n.  SELECT ItemNumber, Name, UnitPrice, Discounto.  FROM Productsp.  WHERE Discount IS NULL;

Mark is creating a view based on an Employees table. He wants the statement thatcreates the view to be scrambled when added to the database. What code can mark use todo this?

.  CREATE VIEW Payrolla.  SET ENCRYPTION ONb.  ASc.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryd.  FROM Employeese.  GOf.  CREATE VIEW Payrollg.  ASh.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryi.  FROM Employees;j.  ENCRYPT WHEN DONEk.  GOl.  CREATE VIEW Payrollm.  WITH ENCRYPTIONn.  ASo.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryp.  FROM Employeesq.  GOr.  CREATE VIEW Payrolls.  ASt.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryu.  FROM Employees;v.  WITH ENCRYPTIONw.  GOx.  CREATE VIEW Payrolly.  AS

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 37/132

z.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryaa.  FROM Employeesbb.  SET ENCRYPTION ONcc.  GO

You want to allow an employee whose login name is Peter to be able to change the design or code of a table named Employees of the Personnel schema. What code would

allow you to do that?

.  SELECT ALL USERS FROM Personnel.Employeesa.  GRANT ALTER TO Peter;b.  GRANT ALTERc.  ON OBJECT::Personnel.Employeesd.  TO Peter;e.  EXECUTE sp_grant(ALTER)f.  TO Peterg.  ON OBJECT::Personnel.Employees;h.  GRANT PERMISSION ALTERi.  TO Peterj.  ON OBJECT::Personnel.Employees;k.  SET PERMISSION ALTERl.  ON OBJECT::Personnel.Employeesm.  TO Peter;

Jimmy has a table named Employees created in a schema named Personnel. He wants to create a view named Payroll in the same schema but he wants to make sure thatno change in the Employees table is allowed if that change would aff ect the Payroll view.What code can he use to accomplish that?

.  CREATE VIEW Personnel.Payrolla.  DO SCHEMA BINDINGb.  ASc.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryd.  FROM Personnel.Employeese.  GOf.  CREATE VIEW Personnel.Payrollg.  ASh.  WITH SCHEMA BINDINGi.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryj.  FROM Personnel.Employeesk.  GOl.  CREATE VIEW Personnel.Payrollm.  FOR SCHEMABINDINGn.  ASo.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryp.  FROM Personnel.Employeesq.  GOr.  CREATE VIEW Personnel.Payrolls.

 WITH SCHEMABINDINGt.  AS

u.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryv.  FROM Personnel.Employeesw.  GOx.  CREATE VIEW Personnel.Payrolly.  ASz.  SELECT EmployeeNumber, FirstName, LastName, HourlySalaryaa.  FROM Personnel.Employeesbb.  SET SCHEMA BINDING ON

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 38/132

cc.  GO

Harriett has the following table of employees: 

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus smallint,HourlySalary money

);GOINSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95);(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GO

Harriett also has the following table of sales made by employees during a certainperiod: 

CREATE TABLE Commercial.Sales(SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,SaleDate date,Amount money

);GOINSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),

(N'284-680', N'2011-02-15', 4250),(N'281-730', N'2011-02-15', 3675),(N'735-407', N'2011-02-15', 3420),(N'730-704', N'2011-02-15', 3675),(N'284-680', N'2011-02-16', 5500),(N'281-730', N'2011-02-16', 2675),(N'735-407', N'2011-02-16', 4400),(N'730-704', N'2011-02-16', 2605);

GO

Now, Harriett wants to see the highest sales made by employees but the list mustinclude only employees who sold over 4750. What code can she use?

.  SELECT LastName + N', ' + FirstName, MAX(Amount)a.  FROM Commercial.Sales cs JOINb.  Personnel.Employees pe ONc.  cs.EmployeeNumber = pe.EmployeeNumberd.  HAVING MAX(Amount) > 4750.00;e.  GROUP BY LastName + N', ' + FirstNamef.  GOg.  SELECT LastName + N', ' + FirstName, MAX(Amount)h.  FROM Commercial.Sales cs JOINi.  Personnel.Employees pe ONj.  cs.EmployeeNumber = pe.EmployeeNumberk.  GROUP BY LastName + N', ' + FirstName

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 39/132

l.  HAVING SUM(Amount) > 4750.00;m.  GOn.  SELECT LastName + N', ' + FirstName, MAX(Amount)o.  FROM Commercial.Sales cs JOINp.  Personnel.Employees pe ONq.  cs.EmployeeNumber = pe.EmployeeNumberr.  GROUP BY LastName + N', ' + FirstNames.  HAVING MAX(Amount) > 4750.00;t.  GOu.  SELECT LastName + N', ' + FirstName, MAX(Amount)v.  FROM Commercial.Sales cs JOINw.  Personnel.Employees pe ONx.  cs.EmployeeNumber = pe.EmployeeNumbery.  GROUP BY LastName + N', ' + FirstNamez.  WHERE Amount > 4750.00;aa.  GObb.  SELECT LastName + N', ' + FirstName, MAX(Amount)cc.  FROM Commercial.Sales cs JOINdd.  Personnel.Employees pe ONee.  cs.EmployeeNumber = pe.EmployeeNumber

ff.  GROUP BY LastName + N', ' + FirstNamegg.  HAVING MAX(Amount) > 4750.00hh.  WHERE Amount IS NOT NULL;ii.  GO

Charles has the following table of employees: 

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus smallint,HourlySalary money

);GOINSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95);(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GO

He also has the following table of sales made by employees during a certain period: CREATE TABLE Commercial.Sales(SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,SaleDate date,

Amount money);GOINSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),(N'284-680', N'2011-02-15', 4250),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 40/132

(N'281-730', N'2011-02-15', 3675);GO

Charles wants to get the list employees so that each record shows the first name, the last name and the numer of sales the employee made. The list must show the number of sales in incremental order. What code can he use?

.  SELECT pe.LastName [First Name], pe.FirstName [Last Name],a.

 COUNT(cs.Amount) AS [Number of Sales]b.  FROM Commercial.Sales cs JOIN

c.  Personnel.Employees pe ONd.  cs.EmployeeNumber = pe.EmployeeNumbere.  GROUP BY pe.LastName, pe.FirstNamef.  ORDER BY [Number of Sales];g.  SELECT pe.LastName [First Name], pe.FirstName [Last Name],h.  COUNT(*) AS [Number of Sales]i.  FROM Commercial.Sales cs JOINj.  Personnel.Employees pe ONk.  cs.EmployeeNumber = pe.EmployeeNumberl.  GROUP BY Amountm.  ORDER BY Amount;n.  SELECT pe.LastName AS [First Name], pe.FirstName AS [Last Name],o.  COUNT(cs.Amount) AS [Number of Sales]p.  FROM Commercial.Sales cs JOINq.  Personnel.Employees pe ONr.  cs.EmployeeNumber = pe.EmployeeNumbers.  GROUP BY cs.Amountt.  HAVING cs..Amountu.  ORDER BY cs.Amount;v.  SELECT pe.LastName AS [First Name], pe.FirstName AS [Last Name],w.  COUNT(*) AS [Number of Sales]x.  GROUP BY cs.Amounty.  FROM Commercial.Sales cs JOINz.  Personnel.Employees pe ONaa.  cs.EmployeeNumber = pe.EmployeeNumber

bb. 

ORDER BY cs.Amount;cc.  SELECT pe.LastName AS [First Name], pe.FirstName AS [LastName],

dd.  COUNT(cs.Amount) AS [Number of Sales]ee.  ORDER BY cs.Amountff.  GROUP BY cs.Amountgg.  FROM Commercial.Sales cs JOINhh.  Personnel.Employees pe ONii.  cs.EmployeeNumber = pe.EmployeeNumber;

Imagine you have the following table: 

CREATE TABLE Contractors(

EmployeeID int identity(1, 1),

FullName nvarchar(50),Wage money

);

Imagine you want to rename the last column from Wage to HourlySalary. What code would let you do that?

.  RENAME COLUMN N'Contractors.Wage' AS N'Contractors.HourlySalary';

a.  RENAME SET N'Contractors.Wage' AS N'Contractors.HourlySalary';

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 41/132

b.  sp_rename N'Contractors.Wage', N'HourlySalary', N'COLUMN';

c.  EXECUTE sp_changename COLUMN N'Contractors.Wage' TO N'HourlySalary'

d.  RENAME N'Wage' TO N'HourlySalary' FROM Contractors;

Imagine you have a table named Employees that includes a column namedEmploymentStatus but that column has become useless. What code can you use to delete that column?

.  ALTER TABLE Contractorsa.  DELETE COLUMN EmploymentStatus;b.  GOc.  ALTER TABLE Contractorsd.  SET EmploymentStatus = NULL;e.  GOf.  UPDATE TABLE Contractorsg.  DELETE EmploymentStatus;h.  GOi.  ALTER TABLE Contractorsj.  DROP COLUMN EmploymentStatus;k.  GOl.  UPDATE TABLE Contractorsm.  SET EmploymentStatus = NULL;n.  GO

John has a table named Employees and another table named Products. The tables were created using the following code: 

CREATE TABLE Employees(

[Empl #] nchar(7),[First Name] nvarchar(20),[Last Name] nvarchar(20),[Hourly Salary] money

);

GOCREATE TABLE Products(

Number int,Name nvarchar(50),UnitPrice money,

);GO

INSERT INTO EmployeesVALUES(N'207-025', N'Julie', N'Flanell', 36.55),

(N'926-705', N'Paulette', N'Simms', 26.65),(N'240-002', N'Alexandra', N'Ulm', 12.85),

(N'847-295', N'Ellie', N'Tchenko', 11.95);GOINSERT INTO ProductsVALUES(217409, N'Short Black Skirt', 55.85),

(790279, N'Classic Fit Pinpoint Shirt', 82.00),(284001, N'Pencil Skirt', 49.00);

GO

John wants to see a list of employees mixed with products. The list should include only products that cost more than 50.00. What code can he use?

.  SELECT [Empl #], [First Name], [Last Name], Name, UnitPrice

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 42/132

a.  FROM Employees, Productsb.  WHERE UnitPrice > 50;c.  GOd.  SELECT empl.[Empl #], empl.[First Name], empl.[Last Name],e.  empl.Name, DISTINCT(empl.UnitPrice)f.  FROM Employees empl, Products prodg.  WHERE empl.UnitPrice > 50;h.  GOi.  SELECT empl.[Empl #], empl.[First Name], empl.[Last Name],j.  prod.Name, prod.UnitPricek.  FROM Employees empl INNER JOIN Products prodl.  ON empl.[Empl #] = prod.Numberm.  HAVING prod.UnitPrice > 50;n.  GOo.  SELECT empl.[Empl #], empl.[First Name], empl.[Last Name],p.  prod.Name, prod.UnitPriceq.  FROM Employees empl JOIN Products prodr.  ON empl.[Empl #] = prod.Numbers.  GROUP BY prod.UnitPricet.  HAVING prod.UnitPrice > 50;u.  GOv.  SELECT empl.[Empl #], empl.[First Name], empl.[Last Name],w.  prod.Name, prod.UnitPricex.  FROM Employees empl, Products prody.  HAVING prod.UnitPrice > 50;z.  GO

Ashley has a table of employees created as follows: 

CREATE TABLE Employees(EmployeeNumber nchar(7),[First Name] nvarchar(20),[Last Name] nvarchar(20),

[Hourly Salary] money);GO

She wants to create a table named Sales so that each record in the Sales table indicates the employee who made the sale. What two codes can she use to create the new table?

.  CREATE TABLE Salesa.  (b.  SaleID int IDENTITY(1, 1) PRIMARY KEY,c.  SaleDate date,d.  EmployeeNumber nchar(7),e.  ProductName nvarchar(60),f.  UnitPrice money,g.  CONSTRAINT FK_Employees FOREIGN KEY REFERENCES

Employees(EmployeeNumber)h.  );i.  CREATE TABLE Salesj.  (k.  SaleID int IDENTITY(1, 1) PRIMARY KEY,l.  SaleDate date,m.  EmployeeNumber nchar(7) FOREIGN KEY REFERENCES

Employees(EmployeeNumber),n.  ProductName nvarchar(60),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 43/132

o.  UnitPrice money,p.  );q.  CREATE TABLE Salesr.  (s.  SaleID int IDENTITY(1, 1) PRIMARY KEY,t.  SaleDate date,u.  EmployeeNumber nchar(7)v.  CONSTRAINT FK_Employees FOREIGN KEY REFERENCES

Employees(EmployeeNumber),w.  ProductName nvarchar(60),x.  UnitPrice money,y. z.  );aa.  CREATE TABLE Salesbb.  (cc.  SaleID int IDENTITY(1, 1) PRIMARY KEY,dd.  SaleDate date,ee.  EmployeeNumber nchar(7)ff.  CONSTRAINT FOREIGN KEY REFERENCES

Employees(EmployeeNumber),

gg.  ProductName nvarchar(60),hh.  UnitPrice money,ii. jj.  );kk.  CREATE TABLE Salesll.  (mm.  SaleID int IDENTITY(1, 1) PRIMARY KEY,nn.  SaleDate date,oo.  EmployeeNumber nchar(7),pp.  ProductName nvarchar(60),qq.  UnitPrice money,rr.  CREATE FOREIGN KEY WITH EmployeeNumber FROM Employeesss. tt.  );

Imagine you have an index named IDX _Customers on a table named Customers butyou don't need that index anymore. What code can you use to delete that index?

.  DROP INDEX IDX_Customers ON Customers(AccountNumber);a.  DELETE INDEX IDX_Customers ON Customers;b.  DROP INDEX IDX_Customers FROM Customers;c.  DROP INDEX IDX_Customers ON Customers;d.  DELETE INDEX IDX_Customers AccountNumber FROM Customers;

Jason had created two tables as follows: 

CREATE TABLE Employees(EmployeeNumber nchar(7) PRIMARY KEY,

FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);GOCREATE TABLE Sales(

SaleID int IDENTITY(1, 1) PRIMARY KEY,SaleDate date,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 44/132

  ProductName nvarchar(60),UnitPrice money

);GO

Now he wants to add a column to the Sales table so that the new field gets its values from the Employees table. How can he write code to do that?

.  ALTER TABLE Salesa.  ADD FOREIGN KEY EmployeeNumber nchar(7) FOR

Employees(EmployeeNumber);b.  UPDATE TABLE Salesc.  ADD COLUMN EmployeeNumber nchar(7) FOREIGN KEY

Employees(EmployeeNumber);d.  GOe.  CHANGE TABLE Salesf.  ADD EmployeeNumber nchar(7) FOREIGN KEY

Employees(EmployeeNumber);g.  GOh.  UPDATE TABLE Salesi.  ADD FOREIGN KEY AS EmployeeNumber nchar(7)j.  REFERENCES EmployeeNumber FROM Employees;

k.  ALTER TABLE Salesl.  ADD EmployeeNumber nchar(7) FOREIGN KEY REFERENCESEmployees(EmployeeNumber);

Jimmy wants to create a custom data type named Integer and based on the int data type. How can he write to accomplish that?

.  CREATE DATATYPE Integer FROM int;a.  EXECUTE sp_createtype Integer AS Natural;b.  CREATE TYPE SET Integer = int;c.  CREATE OBJECT:TYPE Integer FROM int;d.  CREATE TYPE Integer FROM int;

In order to rightly perform data analysis on a database with international names, Jimmy wants to add a f lag on a string-based column. What code can he use to take or latin-based characters?

.  CREATE TABLE Customersa.  (b.  CustomerID int identity(1, 1),c.  FullName nvarchar(50),d.  PhoneNumber nvarchar(20)e.  )f.  WITH COLLATE SQL_Latin1_General_CP1_CI_AS;g.  WITH COLLATE SQL_Latin1_General_CP1_CI_ASh.  BEGINi.  CREATE TABLE Customersj.  (k.

 CustomerID int identity(1, 1),l.  FullName nvarchar(50),

m.  PhoneNumber nvarchar(20)n.  )o.  END;p.  CREATE TABLE Customersq.  (r.  CustomerID int identity(1, 1),s.  FullName nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS,t.  PhoneNumber nvarchar(20)

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 45/132

u.  );v.  CREATE TABLE Customersw.  (x.  CustomerID int identity(1, 1),y.  FullName nvarchar(50) WITH COLLATE

SQL_Latin1_General_CP1_CI_AS,z.  PhoneNumber nvarchar(20)aa.  );bb.  CREATE TABLE Customerscc.  (dd.  CustomerID int identity(1, 1),ee.  FullName nvarchar(50),ff.  PhoneNumber nvarchar(20),gg.  CONSTRAINT CT_Customers COLLATE

SQL_Latin1_General_CP1_CI_AShh.  );

Michael has a database with a table named Customers. He created a user namedJames Galvin and he wants to give him the ability to create new records or update existingrecords on that table. What code can he use?

.  GRANT INSERT, UPDATEa.  ON OBJECT::Sales.Customersb.  TO [James Galvin];c.  GOd.  GRANT INSERT, UPDATEe.  TO [James Galvin];f.  ON OBJECT::Sales.Customersg.  GOh.  SET GRANT INSERT, UPDATEi.  ON OBJECT::Sales.Customersj.  TO N'James Galvin';k.  GOl.  GRANT INSERT AND UPDATEm.

 FOR OBJECT::Sales.Customersn.  TO [James Galvin];

o.  GOp.  WITH GRANT INSERT, UPDATEq.  SET OBJECT::Sales.Customersr.  TO [James Galvin];s.  GO

Imagine you have already created a login name for an employee as jpalau. Now youthat employee to be able to create databases on the server. What code would you use togive that right?

.  GRANT CREATE DATABASESa.  TO jpalau;b.  FOR ANY DATABASEc.  GRANT CREATE TO jpalau;d.  WITH OBJECT::jpalaue.  GRANT CREATE ANY DATABASE;f.  GRANT CREATE ANY DATABASEg.  TO jpalau;h.  EXECUTE CREATE ANY DATABASEi.  FOR USER::jpalau;

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 46/132

  Mike has already created two login names as gsanders and hnorm for twoemployees. He wants to give each the ability to create databases on the server and be able to change the login account of other employees. What code can he use to take care of that?

.  WITH gsanders, hnorma.  GRANT CREATE ANY DATABASE, ALTER ANY LOGIN;b.  GOc.  GRANT CREATE ANY DATABASE, ALTER ANY LOGINd.  TO gsanders, hnorm;e.  GOf.  GRANT PERMISSION::CREATE ANY DATABASE, PERMISSION::ALTER ANY

LOGINg.  TO gsanders, hnorm;h.  GOi.  GRANT PERMISSION(CREATE ANY DATABASE, ALTER ANY LOGIN)j.  TO OBJECT::gsanders;k.  GOl. m.  GRANT PERMISSION(CREATE ANY DATABASE, ALTER ANY LOGIN)n.  TO OBJECT::hnorm;o.

 GOp.  EXECUTE GRANT PERMISSION

q.  SET CREATE ANY DATABASE = TRUEr.  SET ALTER ANY LOGIN = TRUEs.  TO OBJECT::gsanders;t.  GOu. v.  EXECUTE GRANT PERMISSIONw.  SET CREATE ANY DATABASE = TRUEx.  SET ALTER ANY LOGIN = TRUEy.  TO OBJECT::hnorm;z.  GO

You have a table of products that was created as follows: 

CREATE TABLE Products(

ProductCode nchar(6) not null,Name nvarchar(50) not null,UnitPrice money not null,CONSTRAINT PK_Products PRIMARY KEY(ProductCode)

);

When you add a f ew records, you want to immediately see the list of records thatwere added. What example of code would do that?

.  INSERT Productsa.  VALUES(N'293804', N'Mid Lady Bag - Lizard', 228),b.  (N'400571', N'Holly Gladiator Heel Shoes', 198)c.  OUTPUT inserted.*;d.  INSERT Productse.  VALUES(N'293804', N'Mid Lady Bag - Lizard', 228),f.  (N'400571', N'Holly Gladiator Heel Shoes', 198)g.  SHOW OUTPUT;h.  INSERT Productsi.  OUTPUT inserted.*j.  VALUES(N'293804', N'Mid Lady Bag - Lizard', 228),k.  (N'400571', N'Holly Gladiator Heel Shoes', 198);l.  INSERT Productsm.  SELECT VALUES(N'293804', N'Mid Lady Bag - Lizard', 228),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 47/132

n.  (N'400571', N'Holly Gladiator Heel Shoes', 198);o.  WHEN INSERT Productsp.  VALUES(N'293804', N'Mid Lady Bag - Lizard', 228),q.  (N'400571', N'Holly Gladiator Heel Shoes', 198)r.  GO TO OUTPUT INSERT;

Barbara is creating a table of products that will contain columns named ProductID, 

Name, and UnitPrice. The value for the product number must be an integer automaticallygenerated by the database engine, the name will be a string with a maximum of 50characters, and the unit price will be set to the local currency. What code can she use tocreate the table?

.  CREATE TABLE Productsa.  (b.  ProductID int AUTONUMBER,c.  Name nvarchar(50),d.  UnitPrice moneye.  );f.  GOg.  CREATE TABLE Productsh.  (i.  ProductID int COUNTER,j.  Name nvarchar(50),k.  UnitPrice moneyl.  );m.  GOn.  CREATE TABLE Productso.  (p.  ProductID int AUTOINCREMENT,q.  Name nvarchar(50),r.  UnitPrice moneys.  );t.  GOu.  CREATE TABLE Productsv.  (w.  ProductID int IDENTITY(1, 1),x.  Name nvarchar(50),y.  UnitPrice moneyz.  );aa.  GObb.  CREATE TABLE Productscc.  (dd.  ProductID int,ee.  Name nvarchar(50),ff.  UnitPrice money,gg.  CONSTRAINT AI_Products IDENTITY(ProductID)hh.  );ii.  GO

Diane created a table of products that includes a column named ProductID thatautomatically gets its values from the database engine. The table was created as follows: 

CREATE TABLE Products(

ProductID int identity(1, 1),Name nvarchar(50),UnitPrice money

);

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 48/132

GO

Diane wants to add a new record but she wants to specify the product number. Whatcode can she use to create the table?

.  SET IDENTITY_INSERT Products ON;a.  GOb.  INSERT Products(ProductID, Name, UnitPrice)c.  VALUES(1002, N'Mid Lady Bag - Lizard', 228),d.  (84, N'Holly Gladiator Heel Shoes', 198),e.  (519, N'Short Black Skirt', 55.85);f.  GOg.  WITH IDENTITY_INSERT ON Products;h.  GOi.  INSERT Products(ProductID, Name, UnitPrice)j.  VALUES(1002, N'Mid Lady Bag - Lizard', 228),k.  (84, N'Holly Gladiator Heel Shoes', 198),l.  (519, N'Short Black Skirt', 55.85);m.  GOn.  WITH AUTOINCREMENT = FALSE;o.  GOp.  INSERT Products(ProductID, Name, UnitPrice)q.  VALUES(1002, N'Mid Lady Bag - Lizard', 228),r.  (84, N'Holly Gladiator Heel Shoes', 198),s.  (519, N'Short Black Skirt', 55.85);t.  GOu.  SET IDENTITY ON Products = NULL;v.  GOw.  INSERT Products(ProductID, Name, UnitPrice)x.  VALUES(1002, N'Mid Lady Bag - Lizard', 228),y.  (84, N'Holly Gladiator Heel Shoes', 198),z.  (519, N'Short Black Skirt', 55.85);aa.  INSERT Products(ProductID, Name, UnitPrice)bb.  WITH IDENTITY_INSERT = TRUEcc.  VALUES(1002, N'Mid Lady Bag - Lizard', 228),

dd. 

(84, N'Holly Gladiator Heel Shoes', 198),ee.  (519, N'Short Black Skirt', 55.85);

Justin has a table named Products and that has a f ew records. The table was createdas follows: 

CREATE TABLE Products(

ProductID int identity(1, 1) not null,Name nvarchar(50),Size nvarchar(32),UnitPrice money,DiscountRate decimal(4, 2),CONSTRAINT PK_Products PRIMARY KEY(ProductID)

);

Justin wants to get a list of product names, their sizes and prices. He also wants the list to be sorted alphabetically according to the names. What code can he use to dothat?

.  SELECT Name, Size, UnitPricea.  FROM Productsb.  SORT(Name);c.  GOd.  SELECT SORT(Name), Size, UnitPricee.  FROM Products;

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 49/132

f.  GOg.  SELECT Name, Size, UnitPriceh.  ORDER BY Namei.  FROM Products;j.  GOk.  SELECT Name, Size, UnitPricel.  FROM Productsm.  ORDER BY Name;n.  GOo.  SELECT Name, Size, UnitPricep.  SET SORT FOR Nameq.  FROM Products;r.  GO

Linette has a table named Products and that has a f ew records. The table was created as follows: 

CREATE TABLE Products(

ProductID int identity(1, 1) not null,DateAcquired date,

Name nvarchar(50),Size nvarchar(32),UnitPrice money,DiscountRate decimal(4, 2),CONSTRAINT PK_Products PRIMARY KEY(ProductID)

);GOINSERT Products(DateAcquired, Name, UnitPrice)VALUES(N'2011-12-06', N'Mid Lady Bag - Lizard', 228),

(N'2010-08-09', N'Midnight Floral Cardigan', 78),(N'2011-10-12', N'Zip Front Sheath Dress', 138),(N'2010-05-24', N'Holly Gladiator Heel Shoes', 198),(N'2011-02-16', N'Short Black Skirt', 55.85);

GOLinette wants to get a list of products using with the date they were acquired, theirnames, their sizes and prices. He also wants the list to be sorted by the acquireddates from the most recent to the oldest item. What code can he use to do that?

.  SELECT DateAcquired, Name, Size, UnitPricea.  FROM Productsb.  ORDER BY DateAcquired ASC;c.  GOd.  SELECT DateAcquired, Name, Size, UnitPricee.  FROM Productsf.  ORDER BY DateAcquired DESC;g.  GOh.  SELECT DateAcquired, Name, Size, UnitPricei.  SORT(DateAcquired) ASCENDINGj.  FROM Products;k.  GOl.  SELECT DateAcquired, Name, Size, UnitPricem.  FROM Productsn.  ORDER BY DateAcquired DESCENDING;o.  GOp.  SELECT DateAcquired, Name, Size, UnitPriceq.  SET SORT FOR DateAcquired ASCr.  FROM Products;

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 50/132

s.  GOStan created a table named Products and filled it with records as follows: 

CREATE TABLE Products(

ProductCode nchar(6) not null,DateAcquired date DEFAULT GETDATE(),Name nvarchar(50),Size nvarchar(32),UnitPrice money,DiscountRate decimal(4, 2),CONSTRAINT PK_Products PRIMARY KEY(ProductCode)

);GOINSERT Products(ProductCode, Name, Size, UnitPrice)VALUES(N'274978', N'Mid Lady Bag - Lizard', N'14', 228),

(N'827480', N'Midnight Floral Cardigan', N'12', 78),(N'183518', N'Zip Front Sheath Dress', N'Small', 138),(N'384680', N'Holly Gladiator Heel Shoes', N'7.5', 198),(N'247008', N'Short Black Skirt', N'Medium', 55.85);

GONow he wants to copy all the records and put them in a new table named Sales.What code can he use to do that?

.  SELECT FROM Products INTO Sales;a.  GOb.  COPY * FROM Products INTO Sales;c.  GOd.  GET ALL * INTO Sales FROM Products;e.  GOf.  WITH Products SELECT * INTO Sales;g.  GOh.  SELECT ALL * INTO Sales FROM Products;i.  GO

Adekunle has a table named Departments and another table named Employees. Theywere created and filled as follows: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),(N'PSNL', N'Personnel'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus nvarchar(32),DepartmentCode nchar(6)

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 51/132

  );GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'Part Time', N'RSDV'),

(N'572-384', N'Jeannette', N'Welch', N'Part Time', N'PSNL'),(N'279-242', N'Ann', N'Welch', N'Full Time', N'HMRS'),(N'495-728', N'Robert', N'Simms', N'Part Time', N'RSDV'),(N'382-505', N'Paula', N'Waters', N'Part Time', N'PSNL'),(N'958-057', N'Peter', N'Aut', N'Part Time', N'HMRS'),(N'268-046', N'Martine', N'Nyamoto', N'Part Time', N'ACNT'),(N'400-752', N'James', N'Palau', N'Full Time', N'HMRS'),(N'773-148', N'James', N'Larsen', N'Part Time', N'RSDV');

GO

What code can Ade use to get a list of full-time employees from the human resources department?

.  SELECT * FROM Employeesa.  WHERE (EmploymentStatus = N'Full Time') AND (DepartmentCode =

N'HMRS');b.  GOc.  SELECT * FROM Employeesd.  WHERE (EmploymentStatus = N'Full Time') OR (DepartmentCode =N'HMRS');e.  GOf.  WITH (EmploymentStatus = N'Full Time') OR (DepartmentCode =

N'HMRS')g.  SELECT * FROM Employees;h.  GOi.  WHERE (EmploymentStatus = N'Full Time') AND (DepartmentCode IN

N'HMRS');j.  SELECT * FROM Employeesk.  GOl.  WHERE (EmploymentStatus = N'Full Time') OR (DepartmentCode =

N'HMRS');

m. SELECT * FROM Employeesn.  GO

Daniel has a table named Departments and another table named Employees. Theywere created and filled as follows: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),(N'PSNL', N'Personnel'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 52/132

  EmploymentStatus nvarchar(32),DepartmentCode nchar(6)

);GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'Part Time', N'RSDV'),

(N'572-384', N'Jeannette', N'Welch', N'Part Time', N'PSNL'),(N'279-242', N'Ann', N'Welch', N'Full Time', N'HMRS'),(N'495-728', N'Robert', N'Simms', N'Part Time', N'RSDV'),(N'382-505', N'Paula', N'Waters', N'Part Time', N'PSNL'),(N'958-057', N'Peter', N'Aut', N'Part Time', N'HMRS'),(N'268-046', N'Martine', N'Nyamoto', N'Part Time', N'ACNT'),(N'400-752', N'James', N'Palau', N'Full Time', N'HMRS'),(N'773-148', N'James', N'Larsen', N'Part Time', N'RSDV');

GO

What code can he use to get a list of all part-time employees regardless of theirdepartments and all employees of the the Personnel department regardless of theiremployment status?

.  SELECT * FROM Employeesa.  WHERE (EmploymentStatus = N'Part Time') AND (DepartmentCode =

N'PSNL');b.  GOc.  WHEN (EmploymentStatus = N'Part Time') AND (DepartmentCode =

N'HMRS')d.  SELECT * FROM Employees;e.  GOf.  SELECT * FROM Employeesg.  WHERE (EmploymentStatus = N'Part Time') OR (DepartmentCode =

N'PSNL');h.  GOi.  WHEN (EmploymentStatus = N'Part Time') OR (DepartmentCode IN

N'HMRS');j.  SELECT * FROM Employeesk.

 GOl.  SELECT * FROM Employees

m.  WITH (EmploymentStatus = N'Part Time') AND (DepartmentCode =N'HMRS');

n.  GODaniel has a table named Departments and another table named Employees. They

were created and filled as follows: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),(N'PSNL', N'Personnel'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 53/132

  EmployeeNumber nchar(10) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus nvarchar(32),DepartmentCode nchar(6)

);GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'Part Time', N'RSDV'),

(N'572-384', N'Jeannette', N'Welch', N'Part Time', N'PSNL'),(N'279-242', N'Ann', N'Welch', N'Full Time', N'HMRS'),(N'495-728', N'Robert', N'Simms', N'Part Time', N'RSDV'),(N'382-505', N'Paula', N'Waters', N'Part Time', N'PSNL'),(N'958-057', N'Peter', N'Aut', N'Part Time', N'HMRS'),(N'268-046', N'Martine', N'Nyamoto', N'Part Time', N'ACNT'),(N'400-752', N'James', N'Palau', N'Full Time', N'HMRS'),(N'773-148', N'James', N'Larsen', N'Part Time', N'RSDV'),(N'208-255', N'William', N'Aula', N'Full Time', N'PSNL');

GO

He wants to get a list of part-time employees whose last name start with w. What

code can he write to get that list?.  SELECT * FROM Employeesa.  WHERE (EmploymentStatus = N'Part Time') OR (LastName LIKE N'w%');b.  GOc.  WHEN (EmploymentStatus = N'Part Time') AND (LastName LIKE N'w%')d.  SELECT * FROM Employees;e.  GOf.  WHEN (EmploymentStatus = N'Part Time') OR (LastName LIKE N'w%');g.  SELECT * FROM Employeesh.  GOi.  SELECT * FROM Employeesj.  WHERE (EmploymentStatus = N'Part Time') AND (LastName LIKE

N'w%');

k. GOl.  SELECT * FROM Employees

m.  WITH (EmploymentStatus = N'Part Time') AND (LastName LIKE N'w%');n.  GO

Daniel has a table named Departments and another table named Employees. Theywere created and filled as follows: 

CREATE TABLE Departments(

DepartmentCode nchar(6) not null primary key,DepartmentName nvarchar(50) not null

);GO

INSERT INTO DepartmentsVALUES(N'HMRS', N'Human Resources'),

(N'ACNT', N'Accounting'),(N'PSNL', N'Personnel'),(N'RSDV', N'Research & Development');

GOCREATE TABLE Employees(

EmployeeNumber nchar(10) not null primary key,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 54/132

  FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus nvarchar(32),DepartmentCode nchar(6)

);GOINSERT INTO EmployeesVALUES(N'283-947', N'Timothy', N'White', N'Part Time', N'RSDV'),

(N'572-384', N'Jeannette', N'Welch', N'Part Time', N'PSNL'),(N'279-242', N'Ann', N'Welch', N'Full Time', N'HMRS'),(N'495-728', N'Robert', N'Simms', N'Part Time', N'RSDV'),(N'382-505', N'Paula', N'Waters', N'Part Time', N'PSNL'),(N'958-057', N'Peter', N'Aut', N'Part Time', N'HMRS'),(N'268-046', N'Martine', N'Nyamoto', N'Part Time', N'ACNT'),(N'400-752', N'James', N'Palau', N'Full Time', N'HMRS'),(N'773-148', N'James', N'Larsen', N'Part Time', N'RSDV'),(N'208-255', N'William', N'Aula', N'Full Time', N'PSNL');

GO

He wants to get a list of full-time employees of the human resources department andthe part time employees of the personnel department. What code can he use to get

that result?.  SELECT * FROM Employeesa.  WHERE ((EmploymentStatus = N'Full Time') OR (DepartmentCode =

N'HMRS'))b.  ANDc.  ((EmploymentStatus = N'Part Time') OR (DepartmentCode =

N'PSNL'));d.  GOe.  SELECT * FROM Employeesf.  WHERE ((EmploymentStatus = N'Full Time') AND (DepartmentCode =

N'HMRS'))g.  ORh.  ((EmploymentStatus = N'Part Time') AND (DepartmentCode =

N'PSNL'));i.  GOj.  SELECT * FROM Employeesk.  WHERE ((EmploymentStatus = N'Full Time') AND (DepartmentCode =

N'HMRS'))l.  WITHm.  ((EmploymentStatus = N'Part Time') AND (DepartmentCode =

N'PSNL'));n.  GOo.  SELECT * FROM Employeesp.  WHERE ((EmploymentStatus = N'Full Time') OR (DepartmentCode =

N'HMRS'))q.  ORr.  ((EmploymentStatus = N'Part Time') OR (DepartmentCode =

N'PSNL'));s.  GOt.  SELECT * FROM Employeesu.  WHERE ((EmploymentStatus = N'Full Time') AND (DepartmentCode =

N'HMRS'))v.  ANDw.  ((EmploymentStatus = N'Part Time') AND (DepartmentCode =

N'PSNL'));x.  GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 55/132

  Joel has to create a table to hold employees records. The columns must be definedas follows 

Column Name Data Type Size 

EmployeeID int

FirstName nvarchar 20

LastName nvarchar 20

EmploymentStatus nvarchar 32 

The values of the employment status column must be restristed to Part Time, Full Time, orUnknown. Any other value must be excluded. How can Joel write code to create that table?

.  CREATE TABLE Employeesa.  (b.  EmployeeID int unique,c.  FirstName nvarchar(20),d.  LastName nvarchar(20),e.  EmploymentStatus nvarchar(32)f.  SET EmploymentStatus(N'Full Time', N'Part Time', N'Unknown')g.  );h.  GOi.  CREATE TABLE Employeesj.  (k.  EmployeeID int unique,l.  FirstName nvarchar(20),m.  LastName nvarchar(20),n.  EmploymentStatus nvarchar(32)o.  )WITH EmploymentStatus AS (N'Full Time', N'Part Time',

N'Unknown');p.  GOq. r.  CREATE TABLE Employeess.  (t.  EmployeeID int unique,u.  FirstName nvarchar(20),v.  LastName nvarchar(20),w.  EmploymentStatus nvarchar(32),x.  CONSTRAINT FOR EmploymentStatus IN(N'Full Time', N'Part Time',

N'Unknown'))y.  );z.  GOaa.  CREATE TABLE Employeesbb.  (

cc. 

EmployeeID int unique,dd.  FirstName nvarchar(20),ee.  LastName nvarchar(20),ff.  EmploymentStatus nvarchar(32) SET AS (N'Full Time', N'Part

Time', N'Unknown')gg.  );hh.  GOii.  CREATE TABLE Employeesjj.  (kk.  EmployeeID int unique,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 56/132

ll.  FirstName nvarchar(20),mm.  LastName nvarchar(20),nn.  EmploymentStatus nvarchar(32)oo.  CHECK(EmploymentStatus IN(N'Full Time', N'Part Time',

N'Unknown'))pp.  );qq.  GO

Kellie created a table named Employees and filled it up with some records as follows: 

CREATE TABLE Employees(

EmployeeID int unique,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus nvarchar(32)

CHECK(EmploymentStatus IN(N'Full Time', N'Part Time',N'Unknown'))

);GOINSERT INTO Employees

VALUES(10, N'Timothy', N'White', N'Part Time'),(50, N'Jeannette', N'Welch', N'Part Time'),(36, N'Ann', N'Welch', N'Full Time'),(60, N'Robert', N'Simms', N'Part Time'),(20, N'Paula', N'Waters', N'Part Time'),(80, N'Peter', N'Aut', N'Part Time'),(30, N'Martine', N'Nyamoto', N'Part Time'),(24, N'James', N'Palau', N'Full Time'),(53, N'James', N'Larsen', N'Part Time'),(15, N'William', N'Aula', N'Full Time');

GO

Now he wants to get a list of employees using their IDs from 20 to 40. Whatstatement can help him get that result?

. SELECT * FROM Employeesa.  WHERE EmployeeID BETWEEN 20 AND 40;

b.  GOc.  SELECT * FROM Employeesd.  WHERE EmployeeID IN(20, 40);e.  GOf.  SELECT * FROM Employeesg.  WHERE (EmployeeID >= 20) AND (EmployeeID >= 40);h.  GOi.  SELECT * FROM Employeesj.  WHERE EmployeeID IN (20 TO 40);k.  GOl.  SELECT * FROM Employeesm.  WHERE EmployeeID FROM 20 TO 40;n.  GO

Sanko has a table of empoyees created as follows: 

CREATE TABLE Employees(

EmployeeNumber nchar(6) not null primary key,FirstName nvarchar(20),LastName nvarchar(20) not null

);

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 57/132

GO

He wants to create another table named Sales that has a column namedEmployeeNumber whose values would come from the EmployeeNumber of the Employees table. To prevent people from breaking the relationships among records, he wants to make sure that when a record is deleted from the Employees table, the database engine would display an error. How must he create the new table?

. CREATE TABLE Salesa.  (

b.  SaleCode nchar(10) not null primary key,c.  SaleDate date not null,d.  EmployeeNumber nchar(6)e.  CONSTRAINT FK_Employees FOREIGN KEYf.  REFERENCES Employees(EmployeeNumber),g.  Amount moneyh.  )ON DELETE CASCADE ERROR;i.  GOj.  CREATE TABLE Salesk.  (l.  SaleCode nchar(10) not null primary key,m.  SaleDate date not null,

n.  EmployeeNumber nchar(6) FOREIGN KEYo.  REFERENCES Employees(EmployeeNumber),p.  Amount moneyq.  )ON DELETE SET NULL;r.  GOs.  CREATE TABLE Salest.  (u.  SaleCode nchar(10) not null primary key,v.  SaleDate date not null,w.  EmployeeNumber nchar(6)x.  CONSTRAINT FK_Employees FOREIGN KEYy.  REFERENCES Employees(EmployeeNumber)z.  ON DELETE NO ACTION,

aa. 

Amount moneybb.  );cc.  CREATE TABLE Salesdd.  (ee.  SaleCode nchar(10) not null primary key,ff.  SaleDate date not null,gg.  EmployeeNumber nchar(6) FOREIGN KEYhh.  REFERENCES Employees(EmployeeNumber)ii.  ON DELETE SHOW ERROR,jj.  Amount moneykk.  );ll.  GOmm.  CREATE TABLE Salesnn.  (

oo.  SaleCode nchar(10) not null primary key,pp.  SaleDate date not null,qq.  EmployeeNumber nchar(6) FOREIGN KEYrr.  REFERENCES Employees(EmployeeNumber),ss.  Amount moneytt.  );uu.  WITH DELETE SET REFERENCE ERRORvv.  GO

July has a table of employees as follows: 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 58/132

  CREATE TABLE Employees(

EmployeeNumber nchar(6) not null primary key,FirstName nvarchar(20),LastName nvarchar(20) not null

);GO

Now she is creating a Products table that will contain a column that uses some values from the employees table. If a record of the Employees table is updated, she wants to display an error. How should she create the Products table?

.  CREATE TABLE Productsa.  (b.  ProductCode nchar(8) primary key,c.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber)d.  ON UPDATE SHOW ERROR,e.  DateAcquired date,f.  Name nvarchar(60),g.  UnitPrice moneyh.  );i.  GOj.  CREATE TABLE Productsk.  (l.  ProductCode nchar(8) primary key,m.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber),n.  DateAcquired date,o.  Name nvarchar(60),p.  UnitPrice moneyq.  )ON UPDATE CASCADE ERROR;r.  GOs.  DROP TABLE Products;t.  GOu.

 CREATE TABLE Productsv.  (

w.  ProductCode nchar(9) primary key,x.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber)y.  ON UPDATE SET NULL,z.  DateAcquired date,aa.  Name nvarchar(60),bb.  UnitPrice moneycc.  );dd.  GOee.  CREATE TABLE Productsff.  (gg.  ProductCode nchar(9) primary key,

hh.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCESEmployees(EmployeeNumber)

ii.  ON UPDATE NO ACTION,jj.  DateAcquired date,kk.  Name nvarchar(60),ll.  UnitPrice moneymm.  );nn.  GOoo.  CREATE TABLE Products

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 59/132

pp.  (qq.  ProductCode nchar(9) primary key,rr.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber),ss.  DateAcquired date,tt.  Name nvarchar(60),uu.  UnitPrice moneyvv.  )ON UPDATE SET ERROR;ww.  GO

John is creating a new table named Products after creating an Employees table as follows: 

CREATE TABLE Employees(

EmployeeNumber nchar(6) not null primary key,FirstName nvarchar(20),LastName nvarchar(20) not null

);GO

He wants to make sure that when a record is deleted from the Employees table, any

record of the Products table that was using the correspoding value of the Employees table is deleted. What code can he use to create the Products table?

.  CREATE TABLE Productsa.  (b.  ProductCode nchar(9) primary key,c.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber),d.  DateAcquired date,e.  Name nvarchar(60),f.  UnitPrice moneyg.  )ON DELETE SET DELETE;h.  GOi.  CREATE TABLE Productsj.  (k.  ProductCode nchar(9) primary key,l.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber)m.  ON DELETE CASCADE,n.  DateAcquired date,o.  Name nvarchar(60),p.  UnitPrice moneyq.  );r.  GOs.  CREATE TABLE Productst.  (u.  ProductCode nchar(9) primary key,v.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber),w.  DateAcquired date,x.  Name nvarchar(60),y.  UnitPrice moneyz.  )ON DELETE NO ACTION;aa.  GObb.  CREATE TABLE Productscc.  (dd.  ProductCode nchar(9) primary key,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 60/132

ee.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCESEmployees(EmployeeNumber)

ff.  ON DELETE SET NULL,gg.  DateAcquired date,hh.  Name nvarchar(60),ii.  UnitPrice moneyjj.  );kk.  GOll.  CREATE TABLE Productsmm.  (nn.  ProductCode nchar(9) primary key,oo.  EmployeeNumber nchar(6) FOREIGN KEY REFERENCES

Employees(EmployeeNumber),pp.  DateAcquired date,qq.  Name nvarchar(60),rr.  UnitPrice moneyss.  )ON DELETE SET NULL;tt.  GO

Gio has a table that holds the types of houses in a database he created for a 

customer.The table was structured as follows: 

CREATE TABLE Categories(

CategoryID nchar(4) not null PRIMARY KEY,Category nvarchar(32) not null

);GO

Gio now has to create a new table for the houses. The table must include a columnthat will get the house types for the above table. If a category is changed from the above table, Gio would like the records that were using that category in the newtable to be changed to ref lect the new value. How should he create the new table?

.  CREATE TABLE Housesa.  (b.  Code nchar(11) not null PRIMARY KEY,c.  CategoryID nchar(4)d.  FOREIGN KEY REFERENCES Categories(CategoryID)e.  ON UPDATE SET DELETE,f.  City nvarchar(40),g.  MarketValue moneyh.  );i.  GOj.  CREATE TABLE Housesk.  (l.  Code nchar(11) not null PRIMARY KEY,m.  CategoryID nchar(4)n.  FOREIGN KEY REFERENCES Categories(CategoryID)o.  ON UPDATE CASCADE,p.  City nvarchar(40),q.  MarketValue moneyr.  );s.  GOt.  CREATE TABLE Housesu.  (v.  Code nchar(11) not null PRIMARY KEY,w.  CategoryID nchar(4)x.  FOREIGN KEY REFERENCES Categories(CategoryID),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 61/132

y.  City nvarchar(40),z.  MarketValue moneyaa.  )ON UPDATE NO ACTION;bb.  GOcc.  CREATE TABLE Housesdd.  (ee.  Code nchar(11) not null PRIMARY KEY,ff.  CategoryID nchar(4)gg.  FOREIGN KEY REFERENCES Categories(CategoryID)hh.  ON UPDATE SET NULL,ii.  City nvarchar(40),jj.  MarketValue moneykk.  );ll.  GOmm.  CREATE TABLE Housesnn.  (oo.  Code nchar(11) not null PRIMARY KEY,pp.  CategoryID nchar(4)qq.  FOREIGN KEY REFERENCES Categories(CategoryID),rr.  City nvarchar(40),

ss.  MarketValue moneytt.  )ON UPDATE NULL;uu.  GO

Charlie is creating a real estate database for a new customer. He has already createda table for house types as follows: 

CREATE TABLE HouseTypes(

HouseTypeID int not null PRIMARY KEY,HouseType nvarchar(32) not null

);GO

Now he has to create a table that holds the properties the company sells. The table 

of properties will have a column that specifies the type of house. The value of thatcolumn will come from the above table. When a house type deleted from the firsttable, Charlie wants the corresponding records of the new table to be changed toNULL. How can he create the new table to implement that functionality?

.  CREATE TABLE Propertiesa.  (b.  PropertyNumber nchar(11) not null PRIMARY KEY,c.  HouseTypeID int CONSTRAINT FK_PropTypes FOREIGN KEYd.  REFERENCES HouseTypes(HouseTypeID)e.  ON DELETE CASCADE NULL,f.  City nvarchar(40),g.  MarketValue moneyh.  );i.  GOj.  CREATE TABLE Propertiesk.  (l.  PropertyNumber nchar(11) not null PRIMARY KEY,m.  HouseTypeID int CONSTRAINT FK_PropTypes FOREIGN KEYn.  REFERENCES HouseTypes(HouseTypeID)o.  ON DELETE NO ACTION,p.  City nvarchar(40),q.  MarketValue moneyr.  );

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 62/132

s.  GOt.  CREATE TABLE Propertiesu.  (v.  PropertyNumber nchar(11) not null PRIMARY KEY,w.  HouseTypeID int CONSTRAINT FK_PropTypes FOREIGN KEYx.  REFERENCES HouseTypes(HouseTypeID),y.  City nvarchar(40),z.  MarketValue moneyaa.  )ON DELETE NULL IS TRUE;bb.  GOcc.  CREATE TABLE Propertiesdd.  (ee.  PropertyNumber nchar(11) not null PRIMARY KEY,ff.  HouseTypeID int CONSTRAINT FK_PropTypes FOREIGN KEYgg.  REFERENCES HouseTypes(HouseTypeID)hh.  ON DELETE SET NULL,ii.  City nvarchar(40),jj.  MarketValue moneykk.  );ll.  GO

mm.  CREATE TABLE Propertiesnn.  (oo.  PropertyNumber nchar(11) not null PRIMARY KEY,pp.  HouseTypeID int CONSTRAINT FK_PropTypes FOREIGN KEYqq.  REFERENCES HouseTypes(HouseTypeID),rr.  City nvarchar(40),ss.  MarketValue moneytt.  )ON DELETE SET CASCADE NULL;uu.  GO

Courtney is creating a database for a community organization. The datable will have a table of members where each record is represented by a membership category. To limitthe number of categories, Courney first creates a table of membership categories as follows: 

CREATE TABLE Categories(

CategoryID int not null PRIMARY KEY,Category nvarchar(32) not null

);GO

In the table for members, since Courtney will include a column for the categories, when a record of the Categories table is are updated, she wants the correspondingfield in the members table to receive a NULL value. How should she create the table for the members?

.  CREATE TABLE Membersa.  (b.

 MemberID int unique,c.  Name nvarchar(50),

d.  CategoryID int FOREIGN KEY REFERENCES Categories(CategoryID)e.  ON UPDATE CASCADE NULL,f.  MembershipStatus nvarchar(20)g.  );h.  GOi.  CREATE TABLE Membersj.  (k.  MemberID int unique,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 63/132

l.  Name nvarchar(50),m.  CategoryID int FOREIGN KEY REFERENCES Categories(CategoryID)n.  ON UPDATE NO ACTION,o.  MembershipStatus nvarchar(20)p.  );q.  GOr.  CREATE TABLE Memberss.  (t.  MemberID int unique,u.  Name nvarchar(50),v.  CategoryID int FOREIGN KEY REFERENCES Categories(CategoryID)w.  ON UPDATE SET NULL,x.  MembershipStatus nvarchar(20)y.  );z.  GOaa.  CREATE TABLE Membersbb.  (cc.  MemberID int unique,dd.  Name nvarchar(50),ee.  CategoryID int FOREIGN KEY REFERENCES

Categories(CategoryID),ff.  MembershipStatus nvarchar(20)gg.  )ON UPDATE NULL IS TRUE;hh.  GOii.  CREATE TABLE Membersjj.  (kk.  MemberID int unique,ll.  Name nvarchar(50),mm.  CategoryID int FOREIGN KEY REFERENCES

Categories(CategoryID),nn.  MembershipStatus nvarchar(20)oo.  )ON UPDATE SET NULL;pp.  GO

Douglas is creating a database for his company to manage employees records. One of the details about each employee will be the employment status. Douglas creates a table for employment status as follows: 

CREATE TABLE EmploymentTypes(

EmploymentTypeID int identity(1000, 10) primary key,EmploymenStatus nvarchar(20) not null,

);GOINSERT INTO EmploymentTypes(EmploymenStatus)VALUES(N'Full Time'),(N'Part Time'),(N'Unknown');

GO

To keep track of employees, Douglas starts creating a table as follows: CREATE TABLE Employees(

EmployeeNumber nchar(6) not null primary key,EmploymentTypeID int FOREIGN KEY

REFERENCES EmploymentTypes(EmploymentTypeID)DEFAULT 3,

FirstName nvarchar(20),LastName nvarchar(20) not null,HourlySalary money

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 64/132

);GO

If an employment status is deleted from the EmploymentStatus table, Douglas wouldlike the employees who use that status to get the def ault value. How should he change the Employees table to take care of this requirement?

.  CREATE TABLE Employeesa.

 (b.  EmployeeNumber nchar(6) not null primary key,

c.  EmploymentTypeID int FOREIGN KEYd.  REFERENCES EmploymentTypes(EmploymentTypeID)e.  ON DELETE SET DEFAULTf.  DEFAULT 3,g.  FirstName nvarchar(20),h.  LastName nvarchar(20) not null,i.  HourlySalary moneyj.  );k.  GOl.  CREATE TABLE Employeesm.  (n.  EmployeeNumber nchar(6) not null primary key,

o.  EmploymentTypeID int FOREIGN KEYp.  REFERENCES EmploymentTypes(EmploymentTypeID)q.  DEFAULT 3,r.  FirstName nvarchar(20),s.  LastName nvarchar(20) not null,t.  HourlySalary moneyu.  )ON DELETE DEFAULT = NULL;v.  GOw.  CREATE TABLE Employeesx.  (y.  EmployeeNumber nchar(6) not null primary key,z.  EmploymentTypeID int FOREIGN KEYaa.  REFERENCES EmploymentTypes(EmploymentTypeID)

bb. 

DEFAULT 3cc.  ON DELETE CASCADE DEFAULT,dd.  FirstName nvarchar(20),ee.  LastName nvarchar(20) not null,ff.  HourlySalary moneygg.  );hh.  GOii.  CREATE TABLE Employeesjj.  (kk.  EmployeeNumber nchar(6) not null primary key,ll.  EmploymentTypeID int FOREIGN KEYmm.  REFERENCES EmploymentTypes(EmploymentTypeID)nn.  ON DELETE NO ACTIONoo.  DEFAULT 3,

pp.  FirstName nvarchar(20),qq.  LastName nvarchar(20) not null,rr.  HourlySalary moneyss.  );tt.  GOuu.  CREATE TABLE Employeesvv.  (ww.  EmployeeNumber nchar(6) not null primary key,xx.  EmploymentTypeID int FOREIGN KEY

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 65/132

yy.  REFERENCES EmploymentTypes(EmploymentTypeID)zz.  DEFAULT 3,aaa.  FirstName nvarchar(20),bbb.  LastName nvarchar(20) not null,ccc.  HourlySalary moneyddd.  )ON DELETE DEFAULT IS TRUE;eee.  GO

Joseph is working on a database for a department store. He creates a table for the categories of items sold in the store: 

CREATE TABLE ItemsTypes(

ItemTypeID int identity(1, 1) primary key,ItemType nvarchar(20) not null,

);GOINSERT INTO ItemsTypes(ItemType)VALUES(N'Miscellaneous'),(N'Shirts'),(N'Dresses'),(N'Pants');

GO

In the table of items, there will be a column that gets its values from the ItemsTypes 

table. To start with the items sold in the store, Josephs write the following code without executing it: CREATE TABLE StoreItems(

ItemCode nchar(10) not null primary key,ItemTypeID int FOREIGN KEY

REFERENCES ItemsTypes(ItemTypeID)DEFAULT 1,

Name nvarchar(50),Size nvarchar(20) not null,UnitPrice money

);GO

Sometimes the employees will change the name of a type in the ItemsTypes table.When this happens, Joseph wants the items that use that category to get the def aultvalue. How can Joseph change the code the StoreItems table to take care of this?

.  CREATE TABLE StoreItemsa.  (b.  ItemCode nchar(10) not null primary key,c.  ItemTypeID int FOREIGN KEYd.  REFERENCES ItemsTypes(ItemTypeID)e.  DEFAULT 1,f.  Name nvarchar(50),g.  Size nvarchar(20) not null,h.  UnitPrice moneyi.  ) ON UPDATE DEFAULT = NULL;j.

 GOk.  CREATE TABLE StoreItems

l.  (m.  ItemCode nchar(10) not null primary key,n.  ItemTypeID int FOREIGN KEYo.  REFERENCES ItemsTypes(ItemTypeID)p.  ON UPDATE CASCADE DEFAULTq.  DEFAULT 1,r.  Name nvarchar(50),s.  Size nvarchar(20) not null,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 66/132

t.  UnitPrice moneyu.  );v.  GOw.  CREATE TABLE StoreItemsx.  (y.  ItemCode nchar(10) not null primary key,z.  ItemTypeID int FOREIGN KEYaa.  REFERENCES ItemsTypes(ItemTypeID)bb.  DEFAULT 1,cc.  Name nvarchar(50),dd.  Size nvarchar(20) not null,ee.  UnitPrice moneyff.  )ON UPDATE NO ACTION;gg.  GOhh.  CREATE TABLE StoreItemsii.  (jj.  ItemCode nchar(10) not null primary key,kk.  ItemTypeID int FOREIGN KEYll.  REFERENCES ItemsTypes(ItemTypeID)mm.  DEFAULT 1

nn.  ON UPDATE DEFAULT IS TRUE,oo.  Name nvarchar(50),pp.  Size nvarchar(20) not null,qq.  UnitPrice moneyrr.  );ss.  GOtt.  CREATE TABLE StoreItemsuu.  (vv.  ItemCode nchar(10) not null primary key,ww.  ItemTypeID int FOREIGN KEYxx.  REFERENCES ItemsTypes(ItemTypeID)yy.  ON UPDATE SET DEFAULTzz.  DEFAULT 1,aaa.  Name nvarchar(50),bbb.  Size nvarchar(20) not null,ccc.  UnitPrice moneyddd.  );eee.  GO

John has the following tables of employees and contractors who work for his company: 

CREATE TABLE Employees(

EmployeeNumber nchar(9),FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money,

[Status] nvarchar(20) default N'Employee');GOCREATE TABLE Contractors(

ContractorCode nchar(7),Name1 nvarchar(20),Name2 nvarchar(20),Wage decimal(6, 2),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 67/132

  [Type] nvarchar(20) default N'Contractor');GO

INSERT INTO Employees(EmployeeNumber, FirstName, LastName,HourlySalary)

VALUES(N'2930-4708', N'John', N'Franks', 20.05),(N'8274-9571', N'Peter', N'Sonnens', 10.65),(N'6359-8079', N'Leslie', N'Aronson', 15.88);

GOINSERT INTO Contractors(ContractorCode, Name1, Name2, Wage)VALUES(N'350-809', N'Mary', N'Shamberg', 14.20),

(N'286-606', N'Chryssa', N'Lurie', 20.26);GO

In preparation of payroll, John wants to preview the employees and contractors inone common list. What code can he write to get that list?

.  He can't because some data types in the tables are not compatible 

a.  SELECT * FROM Employees;b.  GOc.  SELECT * FROM Contractors;d.  GOe.  SELECT * FROM Employeesf.  UNIONg.  SELECT * FROM Contractors;h.  GOi.  SELECT * FROM Employees;j.  GOk.  UNIONl.  SELECT * FROM Contractors;m.  GOn.  WITH UNIONo.  SELECT * FROM Employeesp.

 ANDq.  SELECT * FROM Contractors;

r.  GOMarc has the following tables of seasonal employees and contractors who do side 

 jobs for his company: 

CREATE TABLE Seasonals(

Number nchar(9),FName nvarchar(20),LName nvarchar(20),HourlySalary money

);GOCREATE TABLE Contractors(

Code nchar(7),Name1 nvarchar(20),Name2 nvarchar(20),Wage decimal(6, 2)

);GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 68/132

  INSERT INTO SeasonalsVALUES(N'2930-4708', N'John', N'Franks', 20.05),

(N'8274-9571', N'Peter', N'Sonnens', 10.65),(N'6359-8079', N'Leslie', N'Aronson', 15.88);

GOINSERT INTO ContractorsVALUES(N'350-809', N'Mary', N'Shamberg', 14.20),

(N'286-606', N'Chryssa', N'Lurie', 20.26);GO

All these seasonal employees and contractors have been hired by the company. Toprepare their inclusion into the company, Mark creates a new table namedEmployees as follows: CREATE TABLE Employees(

EmployeeNumber nchar(9),FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);GO

Marc wants to add the seasonal employees and the contractors to the Employees table. What code can let him do that?

.  SELECT FROM Seasonals INTO Employeesa.  UNIONb.  SELECT FROM Contractors INTO Employees;c.  GOd.  INSERT INTO Employeese.  SELECT * FROM Seasonalsf.  UNIONg.  SELECT * FROM Contractors;h.  GOi.  UNION ALLj.  SELECT ALL * FROM Seasonalsk.

 ANDl.  SELECT ALL * FROM Contractors

m.  INTO Employees;n.  GOo.  SELECT ALL * FROM Seasonalsp.  ANDq.  SELECT ALL * FROM Contractorsr.  INTO Employeess.  UNION ALL;t.  GOu.  SELECT ALL * FROM Seasonals, Contractorsv.  INTO Employeesw.  UNION ALL;x.  GO

George has the following tables with records: 

CREATE TABLE Contractors(

ContractorCode nchar(10),FName nvarchar(20),LName nvarchar(20),Wage decimal(6, 2)

);

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 69/132

  GOCREATE TABLE Employees(

EmployeeNumber nchar(10),DateHired date,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money,EmploymentStatus nvarchar(20) null

);GOINSERT INTO ContractorsVALUES(N'35080', N'Mary', N'Shamberg', 14.20),

(N'286606', N'Chryssa', N'Lurie', 20.26),(N'415905', N'Ralph', N'Sunny', 15.55);

GOINSERT INTO EmployeesVALUES(N'286018', N'20020426', N'Julie', N'Chance', 12.84, N'Full

Time'),(N'286606', N'19981008', N'Ayinda', N'Kaihibu', 9.52, N'Part

Time'),(N'922620', N'20100815', N'Ann', N'Keans', 20.52, N'Full Time'),(N'415905', N'20061222', N'Godwin', N'Harrison', 18.75, N'Full

Time'),(N'682470', N'20080430', N'Timothy', N'Journ', 21.05, NULL);

GO

He wants to merge the records to add those of the contractors to the Employees table by comparing the employee numbers to the contractors codes. During the operation, if a record from the Contractors table matches an employee number, twoleading 0s will be added to the employeee number to make sure the numbers remainunique (but the other parts of the record will not be changed). Otherwise, the recordshould be added. What code would accomplish this?

.  USING Contractors AS SOURCEa.  MERGE Employees AS TARGETb.  ON (Workers.ContractorCode = Teachers.EmployeeNumber)c.  WHEN MATCHEDd.  THEN UPDATE SET EmployeeNumber = N'00' + ContractorCodee.  WHEN NOT MATCHED BY SOURCEf.  THEN INSERT(EmployeeNumber, FirstName, LastName,

HourlySalary)g.  VALUES(ContractorCode, FName, LName, Wage);h.  GOi.  USING Contractors AS SOURCEj.  MERGE Employees AS TARGETk.  ON (Workers.ContractorCode = Teachers.EmployeeNumber)l.  WHEN NOT MATCHED BY SOURCEm.  THEN INSERT(EmployeeNumber, FirstName, LastName,

HourlySalary)n.  VALUES(ContractorCode, FName, LName, Wage)o.  WHEN MATCHEDp.  THEN UPDATE SET EmployeeNumber = N'00' + ContractorCode;q.  GOr.  MERGE Employees AS Teacherss.  WHEN MATCHEDt.  THEN UPDATE SET EmployeeNumber = N'00' + ContractorCodeu.  WHEN NOT MATCHED BY SOURCE

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 70/132

v.  THEN INSERT(EmployeeNumber, FirstName, LastName,HourlySalary)

w.  VALUES(ContractorCode, FName, LName, Wage)x.  USING Contractors AS Workersy.  ON (Workers.ContractorCode = Teachers.EmployeeNumber);z.  MERGE Employees AS Teachersaa.  USING Contractors AS Workersbb.  ON (Workers.ContractorCode = Teachers.EmployeeNumber)cc.  WHEN MATCHEDdd.  THEN UPDATE SET EmployeeNumber = N'00' + ContractorCodeee.  WHEN NOT MATCHED BY TARGETff.  THEN INSERT(EmployeeNumber, FirstName, LastName,

HourlySalary)gg.  VALUES(ContractorCode, FName, LName, Wage);hh.  GOii.  WITH MERGE Employees AS Teachersjj.  ON (Workers.ContractorCode = Teachers.EmployeeNumber)kk.  USING Contractors AS Workersll.  WHEN MATCHEDmm.  THEN UPDATE SET EmployeeNumber = N'00' + ContractorCode

nn.  WHEN NOT MATCHED BY SOURCEoo.  THEN INSERT(EmployeeNumber, FirstName, LastName,

HourlySalary)pp.  VALUES(ContractorCode, FName, LName, Wage);qq.  GO

While working for a department store, Evelyne receives two tables that contain lists of items sold in the store. The tables were created as follows: 

CREATE TABLE Products(

ProductNumber int not null,DateAcquired date,Name nvarchar(50),

Size nvarchar(32),UnitPrice money,CONSTRAINT PK_Products PRIMARY KEY(ProductNumber)

);GOCREATE TABLE StoreItems(

ItemCode int identity(1, 1) not null PRIMARY KEY,Arrival date,[Description] nvarchar(50),Value money

);GOINSERT Products

VALUES(2, N'2011-12-06', N'Mid Lady Bag - Lizard', N'12', 228),(888, N'2010-08-09', N'Midnight Floral Cardigan', N'Small', 78),(105583, N'2011-10-12', N'Zip Front Sheath Dress', N'8', 138),(4, N'2010-05-24', N'Holly Gladiator Heel Shoes', N'7.5', 198),(3680, N'2011-02-16', N'Short Black Skirt', N'14', 55.85),(28, NULL, N'Color-Block Chambray Shirt', N'10', 85.25);

GOINSERT StoreItems(Arrival, [Description], Value)VALUES(N'2010-02-22', N'Short-Sleeved Bush Shirt', 59.95),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 71/132

  (N'2011-10-12', N'Zip Front Sheath Dress', 138),(N'2010-05-24', N'Pure Cashmere Sweater', 165),(N'2011-02-16', N'Short Black Skirt', 55.85);

GO

It appears that some records are duplicated. To start, Evelyne wants to merge the records from the StoreItems to the Products tables. During this operation, if a record

from one table matches a record from the other table, the matching record should be removed. Otherwise, if the record is not found in the StoreItems table, it should be added to the Products table. How can she write code to merge the records?

.  USING StoreItems AS Itemsa.  MERGE Products AS Inventoryb.  ON Inventory.ProductNumber = Items.ItemCodec.  WHEN MATCHEDd.  THEN DELETEe.  WHEN NOT MATCHED THENf.  INSERT(ProductNumber, DateAcquired, Name, UnitPrice)g.  VALUES(ItemCode, Arrival, [Description], Value);h.  GOi.  MERGE Products AS Inventoryj.  USING StoreItems AS Itemsk.  ON Inventory.ProductNumber = Items.ItemCodel.  WHEN MATCHED THEN DELETEm.  WHEN NOT MATCHED THENn.  INSERT(ProductNumber, DateAcquired, Name, UnitPrice)o.  VALUES(ItemCode, Arrival, [Description], Value);p.  GOq.  WITH StoreItems AS Itemsr.  MERGE Products AS Inventorys.  ON Inventory.ProductNumber = Items.ItemCodet.  WHEN MATCHEDu.  THEN DELETEv.  WHEN NOT MATCHED THENw.  INSERT(ProductNumber, DateAcquired, Name, UnitPrice)

x. 

VALUES(ItemCode, Arrival, [Description], Value);y.  GOz.  WITH StoreItems AS Itemsaa.  MERGE Products AS Inventorybb.  ON Inventory.ProductNumber = Items.ItemCodecc.  WHEN MATCHED SET NULLdd.  ORee.  INSERT(ProductNumber, DateAcquired, Name, UnitPrice)ff.  VALUES(ItemCode, Arrival, [Description], Value);gg.  GOhh.  SELECT * FROM Products AS Inventoryii.  ANDjj.  SELECT * FROM StoreItems AS Itemskk.  MERGE

ll.  ON Inventory.ProductNumber = StoreItems.ItemCodemm.  IF MATCH THENnn.  DELETEoo.  ELSEpp.  INSERT(ProductNumber, DateAcquired, Name, UnitPrice)qq.  VALUES(ItemCode, Arrival, [Description], Value);rr.  GO

Imagine you have a table as follows: 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 72/132

  CREATE TABLE Rooms(

RoomNumber nchar(10) not null,RoomType nvarchar(20) default N'Bedroom',BedType nvarchar(40) default N'Queen',Rate money default 75.85,Available bit

);GOINSERT INTO Rooms(RoomNumber, BedType, Rate, Available)VALUES(N'104', default, 80.25, 0),

(N'105', N'King', 95.50, 1),(N'108', N'King', 92.50, 1),(N'109', default, 68.95, 0),(N'110', default, 74.95, 1);

GO

How can you write a common table expression to see its records?.  WITH BedRooms ASa.  (b.  SELECT * FROM Rooms

c.  )d. e.  SELECT * FROM BedRooms;f.  GOg.  WITH BedRooms AS SELECT * FROM BedRooms;h.  BEGINi.  SELECT * FROM Roomsj.  ENDk.  SELECT * FROM BedRoomsl.  AS BedRoomsm.  BEGINn.  SELECT * FROM Roomso.  ENDp.  WITH BedRoomsq.  ASr.  BEGINs.  SELECT * FROM Roomst.  ENDu.  SELECT * FROM Rooms AS BedRoomsv.  BEGINw.  SELECT * FROM Bedroomsx.  END

Imagine you have a table as follows: 

CREATE TABLE Rooms(

RoomNumber nchar(10) not null,

RoomType nvarchar(20) default N'Bedroom',BedType nvarchar(40) default N'Queen',Rate money default 75.85,Available bit

);GOINSERT INTO Rooms(RoomNumber, BedType, Rate, Available)VALUES(N'104', default, 80.25, 0),

(N'105', N'King', 95.50, 1),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 73/132

  (N'108', N'King', 92.50, 1),(N'109', default, 68.95, 0),(N'110', default, 74.95, 1);

GO

What code allows you to create a common table expression that includes only Kingbedrooms?

.  CREATE BedRooms(RoomNumber, RoomType, BedType, Rate, Available)a.  AS CTEb.  BEGINc.  SELECT RoomNumber, RoomType, BedType, Rate, Availabled.  FROM Roomse.  WHERE BedType = N'King'f.  ENDg.  SELECT RoomNumber, RoomType, Rate, Available FROM BedRoomsh.  GOi.  SELECT RoomNumber, RoomType, BedType, Rate, Availablej.  FROM Roomsk.  WHERE BedType = N'King'l.  AS BedRooms(RoomNumber, RoomType, BedType, Rate, Available);m.  GOn.  SELECT RoomNumber, RoomType, Rate, Available FROM BedRoomso.  GOp.  WITH BedRooms(RoomNumber, RoomType, BedType, Rate, Available)q.  ASr.  (s.  SELECT RoomNumber, RoomType, BedType, Rate, Availablet.  FROM Roomsu.  WHERE BedType = N'King'v.  )w.  SELECT RoomNumber, RoomType, Rate, Available FROM BedRoomsx.  GOy.  SET BedRooms(RoomNumber, RoomType, BedType, Rate, Available)z.  BEGINaa.

 SELECT RoomNumber, RoomType, BedType, Rate, Availablebb.  FROM Rooms

cc.  WHERE BedType = N'King'dd.  ENDee.  SELECT RoomNumber, RoomType, Rate, Available FROM BedRoomsff.  GOgg.  WITH BedRooms(RoomNumber, RoomType, BedType, Rate, Available)hh.  BEGINii.  SELECT RoomNumber, RoomType, BedType, Rate, Availablejj.  FROM Roomskk.  WHERE BedType = N'King'll.  SELECT RoomNumber, RoomType, Rate, Available FROM BedRoomsmm.  ENDnn.  GO

Lance is creating a database for a hotel. Based on papers provided by the customer, he created two tables as follows: 

CREATE TABLE SleepingRooms (RoomNumber nchar(10) not null,RoomType nvarchar(20) default N'Bedroom',BedType nvarchar(40) default N'Queen',Rate money default 75.85,Available bit

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 74/132

  );GO

CREATE TABLE ConferenceRooms (RoomNumber nchar(10) not null,RoomType nvarchar(20) default N'Conference',BedType nvarchar(40),Rate money default 75.85,Available bit

);GO

INSERT INTO SleepingRooms(RoomNumber, BedType, Rate, Available)VALUES(N'104', default, 80.25, 0),

(N'105', N'King', 95.50, 1),(N'108', N'King', 92.50, 1),(N'109', default, 68.95, 0),(N'110', default, 74.95, 1);

GO

INSERT INTO ConferenceRooms(RoomNumber, Rate)VALUES(N'C-120', 525.00),

(N'C-122', 450.00);GO

How can he create a common table expression to get a preview of bedrooms andconf erence rooms in one list?

.  WITH HotelRoomsa.  BEGINb.  SELECT * FROM SleepingRoomsc.  UNIONd.  SELECT * FROM ConferenceRoomse.  ENDf.  SELECT * FROM HotelRooms;g.

 GOh.  SET CTE

i.  BEGINj.  SELECT * FROM SleepingRoomsk.  UNIONl.  SELECT * FROM ConferenceRoomsm.  ENDn.  WITH HotelRoomso.  SELECT * FROM HotelRooms;p.  GOq.  DECLARE @HotelRooms AS VIEWr.  BEGINs.  SELECT * FROM SleepingRoomst.  UNION

u.  SELECT * FROM ConferenceRoomsv.  ENDw.  SELECT * FROM @HotelRooms;x.  GOy.  WITH HotelRooms AS TABLEz.  BEGINaa.  SELECT * FROM SleepingRoomsbb.  UNION ALLcc.  SELECT * FROM ConferenceRoomsdd.  END

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 75/132

ee.  SELECT * FROM @HotelRooms;ff.  GOgg.  WITH HotelRoomshh.  ASii.  (jj.  SELECT * FROM SleepingRoomskk.  UNIONll.  SELECT * FROM ConferenceRoomsmm.  )nn.  SELECT * FROM HotelRooms;oo.  GO

Lance is creating a database for a hotel. Based on papers provided by the customer, he created two tables as follows: 

CREATE TABLE SleepingRooms(

RoomNumber nchar(10) not null,RoomType nvarchar(20) default N'Bedroom',BedType nvarchar(40) default N'Queen',Rate money default 75.85,

Available bit);GO

CREATE TABLE ConferenceRooms(

RoomNumber nchar(10) not null,RoomType nvarchar(20) default N'Conference',BedType nvarchar(40),Rate money default 75.85,Available bit

);GO

INSERT INTO SleepingRooms(RoomNumber, BedType, Rate, Available)VALUES(N'104', default, 80.25, 0),

(N'105', N'King', 95.50, 1),(N'108', N'King', 92.50, 1),(N'109', default, 68.95, 0),(N'110', default, 74.95, 1);

GO

INSERT INTO ConferenceRooms(RoomNumber, Rate)VALUES(N'C-120', 525.00),

(N'C-122', 450.00);GO

How can he create a common table expression that shows a list available bedrooms 

and conf erence rooms?.  SET HotelRoomsa.  BEGINb.  SELECT * FROM SleepingRoomsc.  UNIONd.  SELECT * FROM ConferenceRoomse.  ENDf.  SELECT RoomNumber, RoomType, BedType, Rateg.  FROM HotelRooms

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 76/132

h.  WHERE Available = 1;i.  GOj.  WITH HotelRoomsk.  ASl.  (m.  SELECT * FROM SleepingRoomsn.  UNIONo.  SELECT * FROM ConferenceRoomsp.  )q.  SELECT RoomNumber, RoomType, BedType, Rater.  FROM HotelRoomss.  WHERE Available = 1;t.  GOu.  WITH HotelRoomsv.  BEGINw.  SELECT * FROM SleepingRoomsx.  UNIONy.  SELECT * FROM ConferenceRoomsz.  ENDaa.  AS

bb.  SELECT RoomNumber, RoomType, BedType, Ratecc.  FROM HotelRoomsdd.  WHERE Available = 1;ee.  GOff.  WITH HotelRoomsgg.  MERGE AShh.  (ii.  SELECT * FROM SleepingRoomsjj.  UNIONkk.  SELECT * FROM ConferenceRoomsll.  )mm.  SELECT RoomNumber, RoomType, BedType, Ratenn.  FROM HotelRoomsoo.  WHERE Available = 1;pp.  GOqq.  WITH HotelRoomsrr.  MERGE ASss.  BEGINtt.  SELECT * FROM SleepingRoomsuu.  WHERE Available = 1vv.  UNIONww.  SELECT * FROM ConferenceRoomsxx.  WHERE Available = 1yy.  ENDzz.  SELECT RoomNumber, RoomType, BedType, Rateaaa.  FROM HotelRooms;bbb.  GO

Consider the following table: 

CREATE TABLE Rooms(

RoomNumber nchar(10) not null,RoomType nvarchar(20) default N'Bedroom',BedType nvarchar(40) default N'Queen',Rate money default 75.85,Available bit

);

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 77/132

  GOINSERT INTO SleepingRooms(RoomNumber, BedType, Rate, Available)VALUES(N'104', default, 80.25, 0),

(N'105', N'King', 95.50, 1),(N'108', N'King', 92.50, 1),(N'109', default, 68.95, 0),(N'110', default, 74.95, 1);

GO

Write an inline table-valued function that would produce all records of that table .  CREATE FUNCTION GetRooms() AS TABLEa.  RETURNb.  SELECT ALL * FROM Rooms;c.  GOd.  CREATE FUNCTION GetRooms()e.  RETURNf.  SELECT ALL * FROM Rooms AS TABLE;g.  GOh.  CREATE FUNCTION GetRooms()i.  RETURNS TABLEj.  BEGINk.  SELECT ALL * FROM Rooms;l.  ENDm.  GOn.  CREATE FUNCTION GetRooms()o.  RETURNS TABLEp.  ASq.  RETURN SELECT ALL * FROM Rooms;r.  GOs.  CREATE FUNCTION GetRooms()t.  BEGINu.  RETURN SELECT ALL * FROM Rooms;v.  ENDw.  GO

While managing a furniture store, Elise has a table of employees and their sales as follows: 

CREATE SCHEMA Personnel;GOCREATE SCHEMA Commercial;GO

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus smallint,

HourlySalary money);GOCREATE TABLE Commercial.Sales(

SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,SaleDate date,Amount money

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 78/132

  );GOINSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95),(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GO

INSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),(N'284-680', N'2011-02-15', 4250),(N'281-730', N'2011-02-15', 3675);

GO

Instead of a view, she wants to create an inline table-valued function namedGetSales that can produce a list of all sales made by the employees. Each record will include the employee's full name (the last name followed by a comma and followed

by the first name), the date a sale was made, and the amount of the sale. How canshe write code for that function?

.  CREATE FUNCTION GetSales()a.  AS TABLEb.  RETURNc.  SELECT pe.LastName + N', ' + pe.FirstName,d.  cs.SaleDate, cs.Amounte.  FROM Commercial.Sales csf.  INNER JOIN Personnel.Employees peg.  ON cs.EmployeeNumber = pe.EmployeeNumber;h.  GOi.  CREATE FUNCTION GetSales()j.  RETURN TABLEk.

 BEGINl.  RETURNS

m.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],n.  cs.SaleDate, cs.Amounto.  FROM Commercial.Sales csp.  INNER JOIN Personnel.Employees peq.  ON cs.EmployeeNumber = pe.EmployeeNumber;r.  ENDs.  GOt.  CREATE INLINE FUNCTION GetSales()u.  RETURN TABLEv.  BEGINw.  SELECT pe.LastName + N', ' + pe.FirstName,x.  cs.SaleDate, cs.Amount

y.  FROM Commercial.Sales csz.  INNER JOIN Personnel.Employees peaa.  ON cs.EmployeeNumber = pe.EmployeeNumber;bb.  ENDcc.  GOdd.  CREATE FUNCTION GetSales()ee.  RETURNS TABLEff.  ASgg.  RETURN

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 79/132

hh.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],ii.  cs.SaleDate, cs.Amountjj.  FROM Commercial.Sales cskk.  INNER JOIN Personnel.Employees pell.  ON cs.EmployeeNumber = pe.EmployeeNumber;mm.  GOnn.  CREATE FUNCTION GetSales()oo.  BEGINpp.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],qq.  cs.SaleDate, cs.Amountrr.  FROM Commercial.Sales csss.  INNER JOIN Personnel.Employees pett.  ON cs.EmployeeNumber = pe.EmployeeNumber;uu.  ENDvv.  RETURN TABLEww.  GO

While managing a furniture store, Elise has a table of employees and their sales as follows: 

CREATE SCHEMA Personnel;

GOCREATE SCHEMA Commercial;GO

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus smallint,HourlySalary money

);GO

INSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),(N'730-704', N'June', N'Malea', 1, 9.95),(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GO

CREATE TABLE Commercial.Sales(

SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,SaleDate date,Amount money

);

GOINSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),(N'284-680', N'2011-02-15', 4250),(N'281-730', N'2011-02-15', 3675);

GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 80/132

Since views don't allow parameterized queries, she wants to create an inline table-valued function that takes an employee number as argument and produces the sales made by that employee. Each record will include the employee's full name (the lastname followed by a comma and followed by the first name), the date a sale was made, and the amount of the sale. How can she write code to produce that result?

.  CREATE FUNCTION Commercial.GetSales(@EmplNbr nchar(7))a.

 RETURNS TABLEb.  AS

c.  RETURNd.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],e.  cs.SaleDate, cs.Amountf.  FROM Commercial.Sales csg.  INNER JOIN Personnel.Employees peh.  ON cs.EmployeeNumber = pe.EmployeeNumberi.  WHERE pe.EmployeeNumber = @EmplNbr;j.  GOk.  CREATE FUNCTION Commercial.GetSales(@EmplNbr nchar(7))l.  RETURNm.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],n.  cs.SaleDate, cs.Amount

o.  FROM Commercial.Sales csp.  INNER JOIN Personnel.Employees peq.  ON cs.EmployeeNumber = pe.EmployeeNumberr.  WHERE pe.EmployeeNumber = @EmplNbrs.  RETURNS TABLEt.  ENDu.  GOv.  CREATE INLINE FUNCTION Commercial.GetSales(@EmplNbr nchar(7)) AS

TABLEw.  BEGINx.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],y.  cs.SaleDate, cs.Amountz.  FROM Commercial.Sales cs

aa. 

INNER JOIN Personnel.Employees pebb.  ON cs.EmployeeNumber = pe.EmployeeNumbercc.  WHERE pe.EmployeeNumber = @EmplNbr;dd.  ENDee.  GOff.  CREATE FUNCTION Commercial.GetSales(@EmplNbr nchar(7))gg.  RETURNS TABLEhh.  BEGINii.  SELECT pe.LastName + N', ' + pe.FirstName,jj.  cs.SaleDate, cs.Amountkk.  FROM Commercial.Sales csll.  INNER JOIN Personnel.Employees pemm.  ON cs.EmployeeNumber = pe.EmployeeNumbernn.  WHERE pe.EmployeeNumber = @EmplNbr;

oo.  ENDpp.  GOqq.  CREATE FUNCTION Commercial.GetSales(@EmplNbr nchar(7))rr.  BEGINss.  SELECT pe.LastName + N', ' + pe.FirstName AS [Full Name],tt.  cs.SaleDate, cs.Amountuu.  FROM Commercial.Sales csvv.  INNER JOIN Personnel.Employees peww.  ON cs.EmployeeNumber = pe.EmployeeNumber

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 81/132

xx.  WHERE pe.EmployeeNumber = @EmplNbryy.  AS TABLE;zz.  GO

Evelyne works for a department store. She has created a table of items sold in the store and filled it with some records as follows: 

CREATE SCHEMA Inventory;GOCREATE TABLE Inventory.StoreItems(

ItemCode int identity(1, 1) not null PRIMARY KEY,DateAcquired date,Name nvarchar(50),Size nvarchar(32),UnitPrice money,DiscountRate decimal

);GOINSERT Inventory.StoreItems(DateAcquired, Name, Size, UnitPrice,

DiscountRate)

VALUES(N'2011-12-06', N'Mid Lady Bag - Lizard', N'12', 228, NULL),(N'2010-02-22', N'Short-Sleeved Bush Shirt', N'Medium', 59.95,

20),(N'2011-10-12', N'Zip Front Sheath Dress', N'8', 138, NULL),(N'2010-05-24', N'Holly Gladiator Heel Shoes', N'7.5', 198, 40),(N'2011-02-16', N'Short Black Skirt', N'14', 55.85, 0.00),(N'2011-04-18', N'Color-Block Chambray Shirt', N'10', 85.25,

20);GO

To make easy to show the records, Evelyne wants to create a stored produce thatwould produce the item code, the name, the size, and the unit price. How can she write code to create that procedure?

.  CREATE OBJECT::Inventory.GetItemsa.  AS PROCEDUREb.  SELECT ItemCode, Name, Size, UnitPricec.  FROM Inventory.StoreItems;d.  GOe.  CREATE PROCEDURE Inventory.GetItemsf.  ASg.  SELECT ItemCode, Name, Size, UnitPriceh.  FROM Inventory.StoreItems;i.  GOj.  CREATE OBJECT::Inventory.GetItems AS PROCEDUREk.  BEGINl.  SELECT ItemCode, Name, Size, UnitPricem.  FROM Inventory.StoreItems;n.  ENDo.  GOp.  CREATE PROCEDURE OBJECT::Inventory.GetItemsq.  RETURNr.  SELECT ItemCode, Name, Size, UnitPrices.  FROM Inventory.StoreItems;t.  GOu.  CREATE OBJECT::Inventory.GetItemsv.  RETURN PROCEDUREw.  BEGIN

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 82/132

x.  SELECT ItemCode, Name, Size, UnitPricey.  FROM Inventory.StoreItems;z.  ENDaa.  GO

Evelyne works for a department store. She created a stored named GetItems thatbelongs to a scheman named Inventory. What code can she write to see the results of that

stored procedure?

.  SELECT * FROM Inventory.GetItems;a.  GOb.  WITH Inventory.GetItemsc.  EXECUTE;d.  GOe.  EXECUTE Inventory.GetItems;f.  GOg.  USE Inventory.GetItems;h.  GOi.  SET Inventory.GetItems ON;j.  GO

Martha is working for an apartment building. She inherited the following table: CREATE SCHEMA Listing;GOCREATE TABLE Listing.Apartment(

UnitNumber nchar(8) not null,Bedrooms int,Bathrooms real,Price money,Deposit money,Available bit

);

To protect the table, Martha wants to create a stored procedure named GetUnits that

other employees can use to see a list derived from the table. The list will include the unit number, the number of bedrooms, the number of bathrooms, and pricce.Because other people cannot directly execute the new stored procedure, she wants to allow them to use the marthag account. How should she create the storedprocedure to make this possible?

.  CREATE OBJECT::PROCEDURE Registration.GetUnitsa.  ASb.  SET NOCOUNT ONc.  SELECT UnitNumber, Bedrooms, Bathrooms, Priced.  FROM Listing.Apartmentse.  WITH EXECUTE AS N'marthag';f.  GOg.  CREATE Registration.GetUnitsh.

 AS PROCEDUREi.  SET NOCOUNT ON

j.  SELECT UnitNumber, Bedrooms, Bathrooms, Pricek.  FROM Listing.Apartmentsl.  WITH EXECUTE AS N'marthag';m.  GOn.  CREATE PROCEDURE Registration.GetUnitso.  SET USER = N'marthag'p.  ASq.  SET NOCOUNT ON

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 83/132

r.  SELECT UnitNumber, Bedrooms, Bathrooms, Prices.  FROM Listing.Apartments;t.  GOu.  CREATE PROCEDURE Registration.GetUnitsv.  ASw.  SET NOCOUNT ONx.  SELECT UnitNumber, Bedrooms, Bathrooms, Pricey.  FROM Listing.Apartmentsz.  SET USER = N'marthag';aa.  GObb.  CREATE PROCEDURE Registration.GetUnitscc.  WITH EXECUTE AS N'marthag'dd.  ASee.  SET NOCOUNT ONff.  SELECT UnitNumber, Bedrooms, Bathrooms, Pricegg.  FROM Listing.Apartments;hh.  GO

Martha is working for an apartment building. She inherited the following table and its records: 

CREATE SCHEMA Listing;GOCREATE TABLE Listing.Apartment(

UnitNumber nchar(8) not null,Bedrooms int,Bathrooms real,Price money,Deposit money,Available bit

);INSERT Listing.ApartmentsVALUES('104', 2, 1.00, 1050.00, 300.00, 0),

('306', 3, 2.00, 1350.00, 425.00, 1),('105', 1, 1.00, 885.00, 250.00, 1),('202', 1, 1.00, 950.00, 325.00, 0),('304', 2, 2.00, 1250.00, 300.00, 0),('106', 3, 2.00, 1350.00, 425.00, 1),('308', 0, 1.00, 875.00, 225.00, 1),('203', 1, 1.00, 885.00, 250.00, 1),('204', 2, 2.00, 1125.00, 425.00, 1),('205', 1, 1.00, 1055.00, 350.00, 0);

GO

Martha wants to create a stored procedure named ShowUnit that takes a unitnumber as argument and produces the record of the corresponding apartment. Howcan she write code for that stored procedure?

.  CREATE OBJECT::Listing.ShowUnit @UnitNbr nchar(8)a.  RETURN PROCEDUREb.  ASc.  SET NOCOUNT ONd.  SELECT UnitNumber, Bedrooms, Bathrooms, Price, Deposit,

Availablee.  FROM Listing.Apartmentsf.  WHERE UnitNumber = @UnitNbr;g.  GOh.  CREATE OBJECT::Listing.ShowUnit @UnitNbr nchar(8)

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 84/132

i.  RETURNS PROCEDUREj.  ASk.  SET NOCOUNT ONl.  RETURN SELECT UnitNumber, Bedrooms, Bathrooms,m.  Price, Deposit, Availablen.  FROM Listing.Apartmentso.  WHERE UnitNumber = @UnitNbr;p.  GOq.  CREATE PROCEDURE Listing.ShowUnit @UnitNbr nchar(8)r.  ASs.  SET NOCOUNT ONt.  SELECT UnitNumber, Bedrooms, Bathrooms,u.  Price, Deposit, Availablev.  FROM Listing.Apartmentsw.  WHERE UnitNumber = @UnitNbr;x.  GOy.  CREATE PROCEDURE Listing.ShowUnitz.  ASaa.  DECLARE @UnitNbr nchar(8)bb. cc.  SET NOCOUNT ONdd.  RETURN SELECT UnitNumber, Bedrooms, Bathrooms,ee.  Price, Deposit, Availableff.  FROM Listing.Apartmentsgg.  WHERE UnitNumber = @UnitNbr;hh.  GOii.  CREATE PROCEDURE OBJECT::Listing.ShowUnitjj.  @UnitNbr nchar(8)kk.  BEGINll.  SET NOCOUNT ONmm.  RETURN SELECT UnitNumber, Bedrooms, Bathrooms,nn.  Price, Deposit, Availableoo.  FROM Listing.Apartmentspp.  WHERE UnitNumber = @UnitNbr;qq.  ENDrr.  GO

James is working for a department store. He has created two tables  and added af ewrecords follows: 

CREATE TABLE Inventory.Categories(

CategoryID int identity(1, 1) primary key,Category nvarchar(20) not null

);GOCREATE TABLE Inventory.StoreItems(

ItemNumber nvarchar(10) primary key,CategoryID int foreign key

references Inventory.Categories(CategoryID),ItemName nvarchar(60) not null,Size nvarchar(20),UnitPrice money

);GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 85/132

  INSERT INTO Inventory.Categories(Category)VALUES(N'Men'), (N'Women'), (N'Boys'), (N'Girls'),(N'Miscellaneous');GOINSERT INTO Inventory.StoreItemsVALUES(N'264850', 2, N'Long-Sleeve Jersey Dress', N'Petite', 39.95),

(N'930405', 4, N'Solid Crewneck Tee', N'Medium', 12.95),(N'924515', 1, N'Hooded Full-Zip Sweatshirt', N'S', 69.95),(N'294936', 2, N'Cool-Dry Soft Cup Bra', N'36D', 15.55);

GO

The employees of the company are in charge of creating and changing products records. To keep track of the records added or changed from the StoredItems table, James creates a table as follows: CREATE TABLE Inventory.DatabaseOperations(

OperationID int identity(1,1) NOT NULL,ObjectType nchar(20) default N'Table',ObjectName nvarchar(40),PerformedBy nvarchar(50),ActionPerformed nvarchar(max),TimePerformed datetime,

CONSTRAINT PK_Operations PRIMARY KEY(OperationID));GO

When the record of an item has been changed in the StoreItems table, James wouldlike the DatabaseOperations table to receive a notification. How can James create a trigger to perform that operation?

.  CREATE TRIGGER OBJECT::Inventory.ProductUpdateda.  ON Inventory.StoreItemsb.  FOR UPDATEc.  ASd.  BEGINe.  INSERT INTO Inventory.DatabaseOperations(ObjectType,f.  ObjectName, PerformedBy,

g.  ActionPerformed, TimePerformed)h.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),i.  N'Product was updated', GETDATE())j.  ENDk.  GOl.  CREATE TRIGGER OBJECT::Inventory.ProductUpdatedm.  ON Inventory.StoreItemsn.  FOR UPDATEo.  ASp.  RETURNq.  INSERT INTO Inventory.DatabaseOperations(ObjectType,r.  ObjectName, PerformedBy,s.  ActionPerformed, TimePerformed)t.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),u.  N'Product was updated', GETDATE()),v.  GOw.  CREATE OBJECT::Inventory.ProductUpdatedx.  ON Inventory.StoreItemsy.  RETURNS TRIGGERz.  WITH UPDATEaa.  ASbb.  INSERT INTO Inventory.DatabaseOperations(ObjectType,cc.  ObjectName, PerformedBy,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 86/132

dd.  ActionPerformed, TimePerformed)ee.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),ff.  N'Product was updated', GETDATE()),gg.  GOhh.  CREATE TRIGGER Inventory.ProductUpdatedii.  ON Inventory.StoreItemsjj.  AFTER UPDATEkk.  ASll.  INSERT INTO Inventory.DatabaseOperations(ObjectType,mm.  ObjectName, PerformedBy,nn.  ActionPerformed, TimePerformed)oo.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),pp.  N'Product was updated', GETDATE());qq.  GOrr.  CREATE OBJECT::Inventory.ProductUpdatedss.  ON Inventory.StoreItemstt.  AFTER UPDATEuu.  AS TRIGGERvv.  BEGINww.  INSERT INTO Inventory.DatabaseOperations(ObjectType,

xx.  ObjectName, PerformedBy,yy.  ActionPerformed, TimePerformed)zz.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),aaa.  N'Product was updated', GETDATE())bbb.  ENDccc.  GO

James is working for a department store. He has created two tables  and added af ewrecords follows: 

CREATE TABLE Inventory.Categories(

CategoryID int identity(1, 1) primary key,Category nvarchar(20) not null

);GOCREATE TABLE Inventory.StoreItems(

ItemNumber nvarchar(10) primary key,CategoryID int foreign key

references Inventory.Categories(CategoryID),ItemName nvarchar(60) not null,Size nvarchar(20),UnitPrice money

);GO

INSERT INTO Inventory.Categories(Category)

VALUES(N'Men'), (N'Women'), (N'Boys'), (N'Girls'),(N'Miscellaneous');GOINSERT INTO Inventory.StoreItemsVALUES(N'264850', 2, N'Long-Sleeve Jersey Dress', N'Petite', 39.95),

(N'930405', 4, N'Solid Crewneck Tee', N'Medium', 12.95),(N'924515', 1, N'Hooded Full-Zip Sweatshirt', N'S', 69.95),(N'294936', 2, N'Cool-Dry Soft Cup Bra', N'36D', 15.55);

GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 87/132

The employees regularly create, change, and delete records from the tables. To keep track of operations in the StoredItems table, James creates a table as follows: CREATE TABLE Inventory.DatabaseOperations(

OperationID int identity(1,1) NOT NULL,ObjectType nchar(20) default N'Table',ObjectName nvarchar(40),PerformedBy nvarchar(50),ActionPerformed nvarchar(max),TimePerformed datetime,CONSTRAINT PK_Operations PRIMARY KEY(OperationID)

);GO

To know when a record has been deleted from the StoredItems table, James wants a new record to be added to the DatabaseOperations table. How can James create a trigger to get those notifications?

.  CREATE TRIGGER Inventory.ProductRemoveda.  AFTER DELETEb.  ON Inventory.StoreItemsc.  ASd.  RETURNe.  INSERT INTO Inventory.DatabaseOperations(ObjectType,f.  ObjectName, PerformedBy,g.  ActionPerformed, TimePerformed)h.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),i.  N'Existing product deleted', GETDATE());j.  ENDk.  GOl.  CREATE TRIGGER Inventory.ProductRemovedm.  ON Inventory.StoreItemsn.  AFTER DELETEo.  ASp.  INSERT INTO Inventory.DatabaseOperations(ObjectType,

q. 

ObjectName, PerformedBy,r.  ActionPerformed, TimePerformed)s.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),t.  N'Existing product deleted', GETDATE());u.  GOv.  CREATE OBJECT::Inventory.ProductRemovedw.  RETURNS TRIGGERx.  AFTER DELETEy.  ON Inventory.StoreItemsz.  BEGINaa.  RETURNbb.  INSERT INTO Inventory.DatabaseOperations(ObjectType,cc.  ObjectName, PerformedBy,dd.  ActionPerformed, TimePerformed)

ee.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),ff.  N'Existing product deleted', GETDATE());gg.  ENDhh.  GOii.  CREATE OBJECT::Inventory.ProductRemovedjj.  AS TRIGGERkk.  AFTER DELETEll.  ON Inventory.StoreItemsmm.  BEGIN

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 88/132

nn.  INSERT INTO Inventory.DatabaseOperations(ObjectType,oo.  ObjectName, PerformedBy,pp.  ActionPerformed, TimePerformed)qq.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),rr.  N'Existing product deleted', GETDATE());ss.  ENDtt.  GOuu.  CREATE TRIGGER Inventory.ProductRemovedvv.  AFTER DELETEww.  ON Inventory.StoreItemsxx.  BEGINyy.  INSERT INTO Inventory.DatabaseOperations(ObjectType,zz.  ObjectName, PerformedBy,aaa.  ActionPerformed, TimePerformed)bbb.  VALUES(DEFAULT, N'StoreItems', SUSER_SNAME(),ccc.  N'Existing product deleted', GETDATE());ddd.  ENDeee.  GO

Richard is a member of a team of programmers who have to create a database for a 

bank.To keep track of who creates tables in the project, he creates a table as follows: CREATE SCHEMA Management;GOCREATE TABLE Management.Operations(

OperationID int identity(1,1) NOT NULL,ObjectType nchar(20) default N'Table',Employee nvarchar(50),ActionPerformed nvarchar(max),OccurredOn datetime,CONSTRAINT PK_Operations PRIMARY KEY(OperationID)

);GO

How can he create a trigger that creates a notification in the above table every time a table is created?.  CREATE TRIGGER ObjectAddeda.  WHEN CREATE_TABLEb.  RETURN TABLEc.  ASd.  BEGINe.  INSERT INTO Management.Operations(ObjectType, Employee,f.  ActionPerformed, OccurredOn)g.  VALUES(DEFAULT, SUSER_SNAME(),h.  N'Created a new table', GETDATE());i.  ENDj.  GOk.  CREATE TRIGGER ObjectAddedl.  ON DATABASEm.  FOR CREATE_TABLEn.  ASo.  BEGINp.  INSERT INTO Management.Operations(ObjectType, Employee,q.  ActionPerformed, OccurredOn)r.  VALUES(DEFAULT, SUSER_SNAME(),s.  N'Created a new table', GETDATE());t.  END

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 89/132

u.  GOv.  CREATE TRIGGER ObjectAddedw.  TARGET DATABASEx.  WHEN CREATE_TABLEy.  ASz.  INSERT INTO Management.Operations(ObjectType, Employee,aa.  ActionPerformed, OccurredOn)bb.  VALUES(DEFAULT, SUSER_SNAME(),cc.  N'Created a new table', GETDATE());dd.  ENDee.  GOff.  CREATE OBJECT::ObjectAddedgg.  RETURN TRIGGERhh.  WHEN CREATE_TABLEii.  AS TABLEjj.  BEGINkk.  SELECT INTO Management.Operationsll.  SET ObjectType = N'Table',mm.  Employee = SUSER_SNAME(),nn.  ActionPerformed = N'Created a new table',

oo.  OccurredOn = GETDATE();pp.  ENDqq.  GOrr.  CREATE OBJECT::ObjectAddedss.  RETURN TRIGGERtt.  WHEN CREATE_TABLEuu.  AS TABLEvv.  BEGINww.  INSERT INTO Management.Operations(ObjectType, Employee,xx.  ActionPerformed, OccurredOn)yy.  VALUES(DEFAULT, SUSER_SNAME(),zz.  N'Created a new table', GETDATE());aaa.  ENDbbb.  GO

Florence is a member of a team of database developers who work for a bank. The database already contains many ob jects such as tables, views, functions, etc. To find outwhen somebody creates, changes, or deletes an ob ject, she creates a table as follows: 

CREATE SCHEMA Management;GOCREATE TABLE Management.Operations(

OperationID int identity(1,1) NOT NULL,ObjectType nchar(20) default N'Table',Employee nvarchar(50),ActionPerformed nvarchar(max),OccurredOn datetime,

CONSTRAINT PK_Operations PRIMARY KEY(OperationID));

GO

Now she wants to create a trigger that gets a notification if an employees deletes one of the existing tables of the project. How can she create that trigger?

.  CREATE TRIGGER TabledRemoveda.  WHEN DROP_TABLEb.  ASc.  INSERT INTO Management.Operations(ObjectType, Employee,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 90/132

d.  ActionPerformed, OccurredOn)e.  VALUES(DEFAULT, SUSER_SNAME(),f.  N'An existing table was deleted', GETDATE());g.  GOh.  CREATE OBJECT::TabledRemovedi.  AS TRIGGERj.  WHEN DROP_TABLEk.  BEGINl.  INSERT INTO Management.Operations(ObjectType, Employee,m.  ActionPerformed, OccurredOn)n.  VALUES(DEFAULT, SUSER_SNAME(),o.  N'An existing table was deleted', GETDATE());p.  ENDq.  GOr.  CREATE TRIGGER::TabledRemoveds.  ON DATABASEt.  WHEN DROP_TABLEu.  BEGINv.  INSERT INTO Management.Operations(ObjectType, Employee,w.  ActionPerformed, OccurredOn)

x.  VALUES(DEFAULT, SUSER_SNAME(),y.  N'An existing table was deleted', GETDATE());z.  ENDaa.  GObb.  CREATE TabledRemovedcc.  RETURNS TRIGGERdd.  ON DATABASEee.  WHEN DROP_TABLEff.  BEGINgg.  INSERT INTO Management.Operations(ObjectType, Employee,hh.  ActionPerformed, OccurredOn)ii.  VALUES(DEFAULT, SUSER_SNAME(),jj.  N'An existing table was deleted', GETDATE());kk.  ENDll.  GOmm.  CREATE TRIGGER TabledRemovednn.  ON DATABASEoo.  FOR DROP_TABLEpp.  ASqq.  BEGINrr.  INSERT INTO Management.Operations(ObjectType, Employee,ss.  ActionPerformed, OccurredOn)tt.  VALUES(DEFAULT, SUSER_SNAME(),uu.  N'An existing table was deleted', GETDATE());vv.  ENDww.  GO

What's the name of the stored procedure used to send a notification email whensomething happens in Microsoft SQL Server?

.  sp_sendmail 

a.  sp_dbsend _mail 

b.  sp_send _dbmail 

c.  sp_senddb_mail 

d.  sp_send _mail 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 91/132

  Hank has the following tables of employees and sales they made during a certainperiod: 

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),

LastName nvarchar(20),EmploymentStatus smallint,HourlySalary money

);GOCREATE TABLE Commercial.Sales(

SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,SaleDate date,Amount money

);GO

INSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95),(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GOINSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),(N'284-680', N'2011-02-15', 4250),(N'281-730', N'2011-02-15', 3675);

GOHank must produce a summary list that shows the employees and the average sale they made during that period. How can he write code to get that result?

.  SELECT EmployeeNumber, AVG(Amount)a.  FROM Salesb.  HAVING EmployeeNumber;c.  SELECT EmployeeNumber, AVG(Amount)d.  FROM Salese.  GROUP BY EmployeeNumber;f.  SELECT DISTINCT EmployeeNumber, AVG(Amount)g.  FROM Sales;h.  GOi.  SELECT EmployeeNumber, AVG(Amount)j.

 FROM Salesk.  WHERE EmployeeNumber IS NOT NULL;

l.  GOm.  SELECT EmployeeNumber, AVG(Amount)n.  FROM Saleso.  GROUP BY Amount;p.  GO

Hank has the following tables of employees and sales they made during a certainperiod: 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 92/132

  CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),EmploymentStatus smallint,HourlySalary money

);GOCREATE TABLE Commercial.Sales(

SaleID int identity(1, 1),EmployeeNumber nchar(7) not null,SaleDate date,Amount money

);GO

INSERT Personnel.EmployeesVALUES(N'284-680', N'Anselme', N'Bongos', 2, 18.62),

(N'730-704', N'June', N'Malea', 1, 9.95),(N'735-407', N'Frank', N'Monson', 3, 14.58),(N'281-730', N'Jerry', N'Beaulieu', 1, 16.65);

GOINSERT INTO Commercial.Sales(EmployeeNumber, SaleDate, Amount)VALUES(N'284-680', N'2011-02-14', 4250),

(N'735-407', N'2011-02-14', 5300),(N'730-704', N'2011-02-14', 2880),(N'281-730', N'2011-02-14', 4640),(N'284-680', N'2011-02-15', 4250),(N'281-730', N'2011-02-15', 3675);

GO

Hank is asked to show a summary list of employees and the average sale they made 

during that period.The list must contain the employee's full name (made of lastname followed by a comma and the first name) and the average sale. How can he 

write code to get that result?.  SELECT e.LastName + N', ' + e.FirstName AS [Full Name],a.  AVG(s.Amount) AS [Average Sales]b.  FROM Sales s INNER JOIN Employees ec.  ON s.EmployeeNumber = e.EmployeeNumberd.  GROUP BY e.LastName + N', ' + e.FirstName;e.  GOf.  SELECT e.LastName + N', ' + e.FirstName AS [Full Name],g.  AVG(s.Amount) AS [Average Sales]h.  FROM Sales s INNER JOIN Employees ei.  ON s.EmployeeNumber = e.EmployeeNumberj.  GROUP BY s.EmployeeNumber;k.  GOl.  SELECT e.EmployeeNumber, AVG(s.Amount) AS [Average Sales]m.  FROM Sales s INNER JOIN Employees en.  ON s.EmployeeNumber = e.EmployeeNumbero.  GROUP BY e.LastName + N', ' + e.FirstName;p.  GOq.  SELECT e.LastName + N', ' + e.FirstName AS [Full Name],r.  AVG(s.Amount) AS [Average Sales]s.  FROM Sales s INNER JOIN Employees e

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 93/132

t.  GROUP BY e.EmployeeNumberu.  ON s.EmployeeNumber = e.EmployeeNumber;v.  GOw.  SELECT e.LastName + N', ' + e.FirstName AS [Full Name],x.  AVG(s.Amount) AS [Average Sales]y.  FROM Sales s INNER JOIN Employees ez.  ON s.EmployeeNumber = e.EmployeeNumberaa.  HAVING e.EmployeeNumber;bb.  GO

Martha has the following list of apartments and the records where the first digit of the unit number indicates its f loor: 

CREATE SCHEMA Listing;GOCREATE TABLE Listing.Apartments(

UnitNumber int not null,Bedrooms int,Bathrooms real,Price money,

Deposit money,Available bit

);GOINSERT Listing.ApartmentsVALUES(104, 2, 1.00, 1050.00, 300.00, 0),

(306, 3, 2.00, 1350.00, 425.00, 1),(105, 1, 1.00, 885.00, 250.00, 1),(202, 1, 1.00, 950.00, 325.00, 0),(304, 2, 2.00, 1250.00, 300.00, 0),(106, 3, 2.00, 1350.00, 425.00, 1),(308, 0, 1.00, 875.00, 225.00, 1),(203, 1, 1.00, 885.00, 250.00, 1),

(204, 2, 2.00, 1125.00, 425.00, 1),(205, 1, 1.00, 1055.00, 350.00, 0);GO

What codes can Martha write to get a list of available apartments from the secondf loor (Select 2)?

.  SELECT * FROM Listing.Apartmentsa.  WHERE UnitNumber IN(200, 299) AND (Available = 1);b.  GOc.  SELECT * FROM Listing.Apartmentsd.  WHERE (UnitNumber >= 200) AND (UnitNumber <= 299) AND (Available

= 1);e.  GOf.  SELECT * FROM Listing.Apartmentsg.  WHERE (UnitNumber BETWEEN 200 AND 299) AND (Available = 1);h.  GOi.  SELECT * FROM Listing.Apartmentsj.  WHERE UnitNumber IN(200, 299) OR (Available = TRUE);k.  GOl.  SELECT * FROM Listing.Apartmentsm.  WHERE UnitNumber >= 200 AND Available IS NOT NULL;n.  GO

Jimmy has a database that contains employees records and their time sheets as follows: 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 94/132

  CREATE SCHEMA Personnel;GOCREATE SCHEMA Payroll;GOCREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);GOCREATE TABLE Payroll.TimeSheets(

TimeSheetID int identity(1, 1) not null primary key,EmployeeNumber nchar(7) not null

foreign key referencesPersonnel.Employees(EmployeeNumber),

DateWorked date,TimeWorked decimal(6, 2)

);GOINSERT Personnel.EmployeesVALUES(N'795-074', N'Steve', N'Leland', 16.46),

(N'240-157', N'Alex', N'Randt', 10.55),(N'482-259', N'Janice', N'Lane', 8.64),(N'628-113', N'Jimmy', N'Walters', 20.24);

GOINSERT INTO Payroll.TimeSheets(EmployeeNumber, DateWorked, TimeWorked)VALUES(N'240-157', N'2011-04-04', 8.00),

(N'628-113', N'2011-04-04', 9.50),(N'795-074', N'2011-04-04', 8.00),(N'482-259', N'2011-04-04', 6.50),(N'795-074', N'2011-04-05', 8.50),(N'482-259', N'2011-04-05', 8.00),(N'240-157', N'2011-04-05', 9.50),(N'628-113', N'2011-04-05', 7.50),(N'795-074', N'2011-04-06', 8.00),(N'240-157', N'2011-04-06', 8.50),(N'795-074', N'2011-04-07', 10.00),(N'628-113', N'2011-04-07', 8.00);

GO

How can he write code to get a list that contains the employee number, the date he or she worked, and the time spent at work, without repeating records?

.  SELECT empl.EmployeeNumber [Empl #],a.  pts.DateWorked [Date Worked],b.  pts.TimeWorked [Time Worked]

c.  FROM Personnel.Employees empl INNER JOIN Payroll.TimeSheetspts;

d.  GOe.  SELECT empl.FirstName [First Name], empl.LastName [Last Name],f.  pts.DateWorked [Date Worked],g.  pts.TimeWorked [Time Worked]h.  FROM Personnel.Employees empl OUTER JOIN Payroll.TimeSheets

pts;i.  GOj.  SELECT empl.EmployeeNumber [Empl #],

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 95/132

k.  pts.DateWorked [Date Worked],l.  pts.TimeWorked [Time Worked]m.  FROM Personnel.Employees empl, Payroll.TimeSheets pts;n.  GOo.  SELECT pts.EmployeeNumber [Empl #],p.  pts.DateWorked [Date Worked],q.  pts.TimeWorked [Time Worked]r.  FROM Payroll.TimeSheets pts;s.  GOt.  SELECT DISTINCT empl.EmployeeNumber [Empl #],u.  pts.DateWorked [Date Worked],v.  pts.TimeWorked [Time Worked]w.  FROM Personnel.Employees empl, Payroll.TimeSheets pts;x.  GO

Jimmy has a database that contains employees records and their time sheets as follows: 

CREATE SCHEMA Personnel;GOCREATE SCHEMA Payroll;

GOCREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);GOCREATE TABLE Payroll.TimeSheets(

TimeSheetID int identity(1, 1) not null primary key,EmployeeNumber nchar(7) not null

foreign key referencesPersonnel.Employees(EmployeeNumber),DateWorked date,TimeWorked decimal(6, 2)

);GOINSERT Personnel.EmployeesVALUES(N'795-074', N'Steve', N'Leland', 16.46),

(N'240-157', N'Alex', N'Randt', 10.55),(N'482-259', N'Janice', N'Lane', 8.64),(N'628-113', N'Jimmy', N'Walters', 20.24);

GOINSERT INTO Payroll.TimeSheets(EmployeeNumber, DateWorked, TimeWorked)VALUES(N'240-157', N'2011-04-04', 8.00),

(N'628-113', N'2011-04-04', 9.50),(N'795-074', N'2011-04-04', 8.00),(N'482-259', N'2011-04-04', 6.50),(N'795-074', N'2011-04-05', 8.50),(N'482-259', N'2011-04-05', 8.00),(N'240-157', N'2011-04-05', 9.50),(N'628-113', N'2011-04-05', 7.50),(N'795-074', N'2011-04-06', 8.00),(N'240-157', N'2011-04-06', 8.50),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 96/132

  (N'795-074', N'2011-04-07', 10.00),(N'628-113', N'2011-04-07', 8.00);

GO

He want to get a list that contains the employee number, the first name, the lastname, the date worked, and the time worked, without repeating records. What code can he write to get that list?

. SELECT empl.EmployeeNumber [Empl #],a.  empl.FirstName [First Name],

b.  empl.LastName [Last Name],c.  pts.DateWorked [Date Worked],d.  pts.TimeWorked [Time Worked]e.  FROM Personnel.Employees empl INNER JOIN Payroll.TimeSheets ptsf.  ON empl.EmployeeNumber = pts.EmployeeNumber;g.  GOh.  SELECT empl.EmployeeNumber [Empl #],i.  empl.FirstName [First Name],j.  empl.LastName [Last Name],k.  pts.DateWorked [Date Worked],l.  pts.TimeWorked [Time Worked]m.  FROM Personnel.Employees empl CROSS JOIN Payroll.TimeSheets pts

n.  ON empl.EmployeeNumber = pts.EmployeeNumber;o.  GOp.  SELECT empl.EmployeeNumber [Empl #],q.  empl.FirstName + N' ' + empl.LastName [Full Name],r.  pts.DateWorked [Date Worked],s.  pts.TimeWorked [Time Worked]t.  FROM Personnel.Employees empl LEFT INNER JOIN

Payroll.TimeSheets ptsu.  ON empl.EmployeeNumber = pts.EmployeeNumber;v.  GOw. x.  SELECT DISTINCT(empl.EmployeeNumber,y.  empl.FirstName,

z. 

empl.LastName),aa.  pts.DateWorked [Date Worked],bb.  pts.TimeWorked [Time Worked]cc.  FROM Personnel.Employees empl LEFT INNER JOIN

Payroll.TimeSheets ptsdd.  ON empl.EmployeeNumber = pts.EmployeeNumber;ee.  GO

ff. gg.  SELECT empl.EmployeeNumber [Empl #],hh.  empl.FirstName [First Name],ii.  empl.LastName [Last Name],jj.  pts.DateWorked [Date Worked],kk.  pts.TimeWorked [Time Worked]ll.  FROM Personnel.Employees empl LEFT JOIN Payroll.TimeSheets

ptsmm.  ON empl.EmployeeNumber = pts.EmployeeNumbernn.  GROUP BY pts.EmployeeNumber;oo.  GO

pp. Leslie has a database that contains employees records and their time sheets as 

follows: 

CREATE SCHEMA Personnel;

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 97/132

  GOCREATE SCHEMA Payroll;GOCREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);GOCREATE TABLE Payroll.TimeSheets(

TimeSheetID int identity(1, 1) not null primary key,EmployeeNumber nchar(7) not null

foreign key referencesPersonnel.Employees(EmployeeNumber),

DateWorked date,TimeWorked decimal(6, 2)

);

GOINSERT Personnel.EmployeesVALUES(N'795-074', N'Steve', N'Leland', 16.46),

(N'240-157', N'Alex', N'Randt', 10.55),(N'482-259', N'Janice', N'Lane', 8.64),(N'628-113', N'Jimmy', N'Walters', 20.24);

GOINSERT INTO Payroll.TimeSheets(EmployeeNumber, DateWorked, TimeWorked)VALUES(N'240-157', N'2011-04-04', 8.00),

(N'628-113', N'2011-04-04', 9.50),(N'795-074', N'2011-04-04', 8.00),(N'482-259', N'2011-04-04', 6.50),(N'795-074', N'2011-04-05', 8.50),(N'482-259', N'2011-04-05', 8.00),(N'240-157', N'2011-04-05', 9.50),(N'628-113', N'2011-04-05', 7.50),(N'795-074', N'2011-04-06', 8.00),(N'240-157', N'2011-04-06', 8.50),(N'795-074', N'2011-04-07', 10.00),(N'628-113', N'2011-04-07', 8.00);

GO

What code can Leslie write to get a list that contains each employee number and the total time the employee worked during that time frame?

.  SELECT pts.EmployeeNumber [Empl #],a.  SUM(pts.TimeWorked [Time Worked])b.  FROM Payroll.TimeSheets ptsc.  GROUP BY pts.EmployeeNumber

d.  WHERE pts.EmployeeNumber IS NOT NULL;e.  GOf. g.  SELECT pts.EmployeeNumber [Empl #],h.  SUM(pts.TimeWorked [Time Worked])i.  FROM Payroll.TimeSheets ptsj.  GROUP BY pts.EmployeeNumberk.  HAVING pts.EmployeeNumber IS NOT NULL;l.  GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 98/132

m. n.  SELECT pts.EmployeeNumber [Empl #],o.  SUM(pts.TimeWorked) [Time Worked]p.  FROM Payroll.TimeSheets ptsq.  GROUP BY pts.EmployeeNumber;r.  GOs.  SELECT pts.EmployeeNumber [Empl #],t.  SUM(pts.TimeWorked) [Time Worked]u.  GROUP BY pts.EmployeeNumberv.  FROM Payroll.TimeSheets pts;w.  GOx.  SELECT pts.EmployeeNumber [Empl #],y.  SUM(pts.TimeWorked) [Time Worked]z.  FROM Payroll.TimeSheets ptsaa.  GROUP BY pts.TimeWorked;bb.  GO

Jimmy has a database that contains employees records and their time sheets as follows: 

CREATE SCHEMA Personnel;

GOCREATE SCHEMA Payroll;GOCREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);GOCREATE TABLE Payroll.TimeSheets(

TimeSheetID int identity(1, 1) not null primary key,EmployeeNumber nchar(7) not nullforeign key references

Personnel.Employees(EmployeeNumber),DateWorked date,TimeWorked decimal(6, 2)

);GOINSERT Personnel.EmployeesVALUES(N'795-074', N'Steve', N'Leland', 16.46),

(N'240-157', N'Alex', N'Randt', 10.55),(N'482-259', N'Janice', N'Lane', 8.64),(N'628-113', N'Jimmy', N'Walters', 20.24);

GO

INSERT INTO Payroll.TimeSheets(EmployeeNumber, DateWorked, TimeWorked)VALUES(N'240-157', N'2011-04-04', 8.00),

(N'628-113', N'2011-04-04', 9.50),(N'795-074', N'2011-04-04', 8.00),(N'482-259', N'2011-04-04', 6.50),(N'795-074', N'2011-04-05', 8.50),(N'482-259', N'2011-04-05', 8.00),(N'240-157', N'2011-04-05', 9.50),(N'628-113', N'2011-04-05', 7.50),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 99/132

  (N'795-074', N'2011-04-06', 8.00),(N'240-157', N'2011-04-06', 8.50),(N'795-074', N'2011-04-07', 10.00),(N'628-113', N'2011-04-07', 8.00);

GO

He want to get a list that contains the employee's name (made of the first name 

followed by space and the last name) and the time worked, without repeatingrecords. What code can he write to get that list?.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],a.  SUM(pts.TimeWorked) [Time Worked]b.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsc.  ON pts.EmployeeNumber = empls.EmployeeNumberd.  GROUP BY empls.FirstName + N' ' + empls.LastName;e.  GOf.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],g.  SUM(pts.TimeWorked) [Time Worked]h.  FROM Payroll.TimeSheets pts LEFT OUTER JOIN Personnel.Employees

emplsi.  ON pts.EmployeeNumber = empls.EmployeeNumberj.  GROUP BY empls.EmployeeNumber;k.  GOl.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],m.  SUM(pts.TimeWorked) [Time Worked]n.  FROM Payroll.TimeSheets pts LEFT OUTER JOIN Personnel.Employees

emplso.  ON pts.EmployeeNumber = empls.EmployeeNumberp.  GROUP BY pts.TimeWorked;q.  GOr.  SELECT DISTINCT empls.FirstName + N' ' + empls.LastName [Full

Name],s.  SUM(pts.TimeWorked) [Time Worked]t.  FROM Payroll.TimeSheets pts CROSS JOIN Personnel.Employees

empls

u. ON pts.EmployeeNumber = empls.EmployeeNumberv.  GROUP BY empls.FirstName + N' ' + empls.LastName;

w.  GOx.  SELECT DISTINCT empls.FirstName + N' ' + empls.LastName [Full

Name],y.  SUM(pts.TimeWorked) [Time Worked]z.  ON pts.EmployeeNumber = empls.EmployeeNumberaa.  FROM Payroll.TimeSheets pts CROSS JOIN Personnel.Employees

emplsbb.  GROUP BY pts.EmployeeNumber;cc.  GO

Bill has a database that contains employees records and their time sheets as follows: 

CREATE SCHEMA Personnel;

GOCREATE SCHEMA Payroll;GOCREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 100/132

  );GOCREATE TABLE Payroll.TimeSheets(

TimeSheetID int identity(1, 1) not null primary key,EmployeeNumber nchar(7) not null

foreign key referencesPersonnel.Employees(EmployeeNumber),

DateWorked date,TimeWorked decimal(6, 2)

);GOINSERT Personnel.EmployeesVALUES(N'795-074', N'Steve', N'Leland', 16.46),

(N'240-157', N'Alex', N'Randt', 10.55),(N'482-259', N'Janice', N'Lane', 8.64),(N'628-113', N'Jimmy', N'Walters', 20.24);

GOINSERT INTO Payroll.TimeSheets(EmployeeNumber, DateWorked, TimeWorked)VALUES(N'240-157', N'2011-04-04', 8.00),

(N'628-113', N'2011-04-04', 9.50),(N'795-074', N'2011-04-04', 8.00),(N'482-259', N'2011-04-04', 6.50),(N'795-074', N'2011-04-05', 8.50),(N'482-259', N'2011-04-05', 8.00),(N'240-157', N'2011-04-05', 9.50),(N'628-113', N'2011-04-05', 7.50),(N'795-074', N'2011-04-06', 8.00),(N'240-157', N'2011-04-06', 8.50),(N'795-074', N'2011-04-07', 10.00),(N'628-113', N'2011-04-07', 8.00);

GO

He want to create a list that shows the employee's name (made of the first name 

followed a space and the last name), the time worked, and the weekly salary thatmulitplies the time worked by the employee's hourly salary. What code can he write to get that list?

.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],a.  SUM(pts.TimeWorked) [Time Worked],b.  SUM(pts.TimeWorked * empls.HourlySalary) [Weekly Salary]c.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsd.  ON pts.EmployeeNumber = empls.EmployeeNumbere.  GROUP BY empls.FirstName + N' ' + empls.LastName;f.  GOg.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],h.  SUM(pts.TimeWorked) [Time Worked],i.  pts.TimeWorked * empls.HourlySalary AS [Weekly Salary]j.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees empls

k.  ON pts.EmployeeNumber = empls.EmployeeNumberl.  GROUP BY empls.FirstName + N' ' + empls.LastName;m.  GOn.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],o.  SUM(pts.TimeWorked) [Time Worked],p.  SUM(pts.TimeWorked) * SUM(empls.HourlySalary) AS [Weekly

Salary]q.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsr.  ON pts.EmployeeNumber = empls.EmployeeNumber

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 101/132

s.  GROUP BY empls.FirstName + N' ' + empls.LastName;t.  GOu.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],v.  SUM(pts.TimeWorked) [Time Worked],w.  MAX(pts.TimeWorked * empls.HourlySalary) AS [Weekly

Salary]x.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsy.  ON pts.EmployeeNumber = empls.EmployeeNumberz.  GROUP BY empls.FirstName + N' ' + empls.LastName;aa.  GObb.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],cc.  SUM(pts.TimeWorked) [Time Worked],dd.  VAL(pts.TimeWorked) * VAL(empls.HourlySalary) AS

[Weekly Salary]ee.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsff.  ON pts.EmployeeNumber = empls.EmployeeNumbergg.  GROUP BY empls.FirstName + N' ' + empls.LastName;hh.  GO

ii. Daouda has a table of employees and another table for their time sheets as follows: CREATE SCHEMA Personnel;GOCREATE SCHEMA Payroll;GO

CREATE TABLE Personnel.Employees(

EmployeeNumber nchar(7) not null primary key,FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money

);

GOCREATE TABLE Payroll.TimeSheets(

TimeSheetID int identity(1, 1) not null primary key,EmployeeNumber nchar(7) not null

foreign key referencesPersonnel.Employees(EmployeeNumber),

DateWorked date,TimeWorked decimal(6, 2)

);GOINSERT Personnel.EmployeesVALUES(N'795-074', N'Steve', N'Leland', 16.46),

(N'240-157', N'Alex', N'Randt', 10.55),

(N'482-259', N'Janice', N'Lane', 8.64),(N'628-113', N'Jimmy', N'Walters', 20.24);

GOINSERT INTO Payroll.TimeSheets(EmployeeNumber, DateWorked, TimeWorked)VALUES(N'240-157', N'2011-04-04', 8.00),

(N'628-113', N'2011-04-04', 9.50),(N'795-074', N'2011-04-04', 8.00),(N'482-259', N'2011-04-04', 6.50),(N'795-074', N'2011-04-05', 8.50),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 102/132

  (N'482-259', N'2011-04-05', 8.00),(N'240-157', N'2011-04-05', 9.50),(N'628-113', N'2011-04-05', 7.50),(N'795-074', N'2011-04-06', 8.50),(N'482-259', N'2011-04-04', 6.50),(N'240-157', N'2011-04-06', 9.50),(N'795-074', N'2011-04-07', 10.00),(N'628-113', N'2011-04-07', 8.00),(N'240-157', N'2011-04-07', 10.00),(N'240-157', N'2011-04-08', 8.50),(N'482-259', N'2011-04-08', 6.00),(N'628-113', N'2011-04-08', 8.00),(N'795-074', N'2011-04-08', 7.50);

GO

To evaluate the overtime of employees, Daouda wants to get a list that contains the employee's name (made of the first name followed by space and the last name) andthe time worked, only for employees who worked over 40 hours. What code can he write to get that list?

.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],a.  SUM(pts.TimeWorked) [Time Worked]

b.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsc.  ON pts.EmployeeNumber = empls.EmployeeNumberd.  GROUP BY empls.FirstName + N' ' + empls.LastNamee.  WHERE SUM(pts.TimeWorked) >= 40.00;f.  GOg.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],h.  SUM(pts.TimeWorked) [Time Worked]i.  FROM Payroll.TimeSheets pts INNER JOIN Personnel.Employees

emplsj.  WHERE SUM(pts.TimeWorked) >= 40.00;k.  ON pts.EmployeeNumber = empls.EmployeeNumberl.  GROUP BY empls.FirstName + N' ' + empls.LastNamem.  GOn.

 SELECT empls.FirstName + N' ' + empls.LastName [Full Name],o.  SUM(pts.TimeWorked) [Time Worked]

p.  FROM Payroll.TimeSheets pts JOIN Personnel.Employees emplsq.  ON pts.EmployeeNumber = empls.EmployeeNumberr.  GROUP BY empls.FirstName + N' ' + empls.LastNames.  HAVING SUM(pts.TimeWorked) >= 40.00;t.  GOu.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],v.  SUM(pts.TimeWorked) [Time Worked]w.  FROM Payroll.TimeSheets pts INNER JOIN Personnel.Employees

emplsx.  HAVING SUM(pts.TimeWorked) >= 40.00;y.  ON pts.EmployeeNumber = empls.EmployeeNumberz.  GROUP BY empls.FirstName + N' ' + empls.LastNameaa.  GObb.  SELECT empls.FirstName + N' ' + empls.LastName [Full Name],cc.  SUM(pts.TimeWorked) [Time Worked]dd.  FROM Payroll.TimeSheets pts INNER JOIN Personnel.Employees

emplsee.  ON pts.EmployeeNumber = empls.EmployeeNumberff.  HAVING SUM(pts.TimeWorked) >= 40.00gg.  GROUP BY empls.FirstName + N' ' + empls.LastName;hh.  GO

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 103/132

  Judith is working on a large database for a commercial bank. The database will be named KoloBank. It will have a primary and secondary log files. Judith will store the records of the database in two hard drives: C: and D:. To prepare to eventually load the records, she creates a folder on the C: drive and names it Kolo Bank Primary. She creates another folder in the D: drive and names it Kolo Bank Secondary. To prepare to store the log files, she creates a folder named Kolo Bank Logs on the C: drive. What code can she use 

to create the database?.  CREATE OBJECT DATABASE KoloBanka.  ON PRIMARYb.  WITH FILEGROUP = KoloBank1c.  ( NAME = N'KoloBankMain',d.  FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',e.  SIZE = 100MB,f.  MAXSIZE = 500MB,g.  FILEGROWTH = 10MB),h.  FILEGROUP KoloBankPrimaryi.  ( NAME = N'KoloBankFirst',j.  FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',k.  SIZE = 20MB,

l.  MAXSIZE = 100MB,m.  FILEGROWTH = 2MB),n.  FILEGROUP = KoloBankSecondaryo.  ( NAME = N'KoloBankSecond',p.  FILENAME = N'D:\Kolo Bank Secondary\KoloBankSecondady.ndf',q.  SIZE = 20MB,r.  MAXSIZE = 100MB,s.  FILEGROWTH = 2MB)t.  LOG ON FILEGROUP = KoloBankLogu.  ( NAME = N'KoloBankLog',v.  FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',w.  SIZE = 10MB,x.  MAXSIZE = 20MB,y.  FILEGROWTH = 2MB);z.  GOaa.  CREATE DATABASE KoloBankbb.  ON PRIMARYcc.  ( NAME = N'KoloBankMain',dd.  FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',ee.  SIZE = 100MB,ff.  MAXSIZE = 500MB,gg.  FILEGROWTH = 10MB),hh.  FILEGROUP KoloBankPrimaryii.  ( NAME = N'KoloBankFirst',jj.  FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',kk.  SIZE = 20MB,ll.  MAXSIZE = 100MB,

mm.  FILEGROWTH = 2MB),nn.  FILEGROUP KoloBankSecondaryoo.  ( NAME = N'KoloBankSecond',pp.  FILENAME = N'D:\Kolo Bank

Secondary\KoloBankSecondady.ndf',qq.  SIZE = 20MB,rr.  MAXSIZE = 100MB,ss.  FILEGROWTH = 2MB)tt.  LOG ONuu.  ( NAME = N'KoloBankLog',

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 104/132

vv.  FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',ww.  SIZE = 10MB,xx.  MAXSIZE = 20MB,yy.  FILEGROWTH = 2MB);zz.  GOaaa.  CREATE OBJECT::KoloBankbbb.  AS DATABASEccc.  ON PRIMARYddd.  WITH FILEGROUP = KoloBank1eee.  ( NAME = N'KoloBankMain',fff.  FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',ggg.  SIZE = 100MB,hhh.  MAXSIZE = 500MB,iii.  FILEGROWTH = 10MB),jjj.  WITH SECONDARY FILEGROUP = KoloBankPrimarykkk.  ( NAME = N'KoloBankFirst',lll.  FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',mmm.  SIZE = 20MB,nnn.  MAXSIZE = 100MB,ooo.  FILEGROWTH = 2MB),

ppp.  WITH SECONDARY FILEGROUP = KoloBankSecondaryqqq.  ( NAME = N'KoloBankSecond',rrr.  FILENAME = N'D:\Kolo Bank

Secondary\KoloBankSecondady.ndf',sss.  SIZE = 20MB,ttt.  MAXSIZE = 100MB,uuu.  FILEGROWTH = 2MB)vvv.  WITH LOG FILEGROUP = KoloBankLogwww.  ( NAME = N'KoloBankLog',xxx.  FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',yyy.  SIZE = 10MB,zzz.  MAXSIZE = 20MB,aaaa.  FILEGROWTH = 2MB);bbbb.  GOcccc.  CREATE DATABASE KoloBankdddd.  ON PRIMARYeeee.  WITH PRIMARY FILEGROUP KoloBank1ffff.  ( NAME = N'KoloBankMain',gggg.  FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',hhhh.  SIZE = 100MB,iiii.  MAXSIZE = 500MB,jjjj.  FILEGROWTH = 10MB),kkkk.  WITH SECONDARY FILEGROUP KoloBankPrimaryllll.  ( NAME = N'KoloBankFirst',mmmm.  FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',nnnn.  SIZE = 20MB,oooo.  MAXSIZE = 100MB,

pppp.  FILEGROWTH = 2MB),qqqq.  WITH SECONDARY FILEGROUP ADD KoloBankSecondaryrrrr.  ( NAME = N'KoloBankSecond',ssss.  FILENAME = N'D:\Kolo Bank

Secondary\KoloBankSecondady.ndf',tttt.  SIZE = 20MB,uuuu.  MAXSIZE = 100MB,vvvv.  FILEGROWTH = 2MB)wwww.  ON LOG FILEGROUP KoloBankLogxxxx.  ( NAME = N'KoloBankLog',

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 105/132

yyyy.  FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',zzzz.  SIZE = 10MB,aaaaa.  MAXSIZE = 20MB,bbbbb.  FILEGROWTH = 2MB);ccccc. GOddddd. CREATE DATABASE KoloBankeeeee. PRIMARY FILEGROUP = KoloBank1fffff.  ( NAME = N'KoloBankMain',ggggg.  FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',hhhhh.  SIZE = 100MB,iiiii.  MAXSIZE = 500MB,jjjjj.  FILEGROWTH = 10MB),kkkkk. SECONDARY FILEGROUP = KoloBankPrimarylllll.  ( NAME = N'KoloBankFirst',mmmmm.  FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',nnnnn.  SIZE = 20MB,ooooo.  MAXSIZE = 100MB,ppppp.  FILEGROWTH = 2MB),qqqqq. SECONDARY FILEGROUP = KoloBankSecondaryrrrrr.  ( NAME = N'KoloBankSecond',

sssss.  FILENAME = N'D:\Kolo BankSecondary\KoloBankSecondady.ndf',

ttttt.  SIZE = 20MB,uuuuu.  MAXSIZE = 100MB,vvvvv.  FILEGROWTH = 2MB)wwwww. ON LOG FILEGROUP = KoloBankLogxxxxx.  ( NAME = N'KoloBankLog',yyyyy.  FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',zzzzz.  SIZE = 10MB,aaaaaa.  MAXSIZE = 20MB,bbbbbb.  FILEGROWTH = 2MB);cccccc.  GO

Brian is working on a large database for a commercial bank. His colleague Judith has 

already created the database as follows: 

CREATE DATABASE KoloBankON PRIMARY( NAME = N'KoloBankMain',FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',SIZE = 100MB,MAXSIZE = 500MB,FILEGROWTH = 10MB),

FILEGROUP KoloBankPrimary( NAME = N'KoloBankFirst',FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',SIZE = 20MB,MAXSIZE = 100MB,

FILEGROWTH = 2MB),FILEGROUP KoloBankSecondary( NAME = N'KoloBankSecond',FILENAME = N'D:\Kolo Bank Secondary\KoloBankSecondady.ndf',SIZE = 20MB,MAXSIZE = 100MB,FILEGROWTH = 2MB)

LOG ON( NAME = N'KoloBankLog',

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 106/132

  FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',SIZE = 10MB,MAXSIZE = 20MB,FILEGROWTH = 2MB);

GO

Because of the large volume of records that the project will have, the database must

be partitioned.To start, Brian has been asked to create a function that will partitionrecords from left to right. The column used to manage the partitions will be of type 

int. What code can Brian use to create the partitions?.  USE KoloBank;a.  GOb.  CREATE PARTITION FUNCTION KoloBankPartitions(int)c.  WITH VALUES(1)d.  IN RANGE LEFT;e.  GOf.  USE KoloBank;g.  GOh.  CREATE FUNCTION KoloBankPartitions(int)i.  RETURNS PARTITIONj.  FOR VALUES(1)k.  AS RANGE LEFT;l.  GOm.  USE KoloBank;n.  GOo.  CREATE OBJECT::FUNCTION KoloBankPartitions(int)p.  AS PARTITIONq.  FOR VALUES(1)r.  AS RANGE LEFT;s.  GOt.  USE KoloBank;u.  GOv.  CREATE PARTITION FUNCTION KoloBankPartitions(int)w.  AS RANGE LEFTx.

 FOR VALUES(1);y.  GO

z.  USE KoloBank;aa.  GObb.  CREATE PARTITION FUNCTION KoloBankPartitions(int)cc.  FOR VALUES(1)dd.  AS RANGE LEFT;ee.  GO

Brian is working on a large database for a commercial bank. His colleague Judith has already created the database as follows: 

CREATE DATABASE KoloBankON PRIMARY( NAME = N'KoloBankMain',FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',SIZE = 100MB,MAXSIZE = 500MB,FILEGROWTH = 10MB),

FILEGROUP KoloBankPrimary( NAME = N'KoloBankFirst',FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',SIZE = 20MB,MAXSIZE = 100MB,

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 107/132

  FILEGROWTH = 2MB),FILEGROUP KoloBankSecondary( NAME = N'KoloBankSecond',FILENAME = N'D:\Kolo Bank Secondary\KoloBankSecondady.ndf',SIZE = 20MB,MAXSIZE = 100MB,FILEGROWTH = 2MB)

LOG ON( NAME = N'KoloBankLog',FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',SIZE = 10MB,MAXSIZE = 20MB,FILEGROWTH = 2MB);

GO

Based on the large volume of records that the project will have, the database mustbe partitioned. A partition function was already created as follows: USE KoloBank;GOCREATE PARTITION FUNCTION KoloBankPartitions(int)AS RANGE LEFT

FOR VALUES(1);GO

To tie the file groups to the scheme, Brian must create a partition function. How canhe write code to create the appropriate partition scheme?

.  USE KoloBank;a.  GOb.  CREATE PARTITION SCHEME KoloBankSchemec.  AS PARTITION KoloBankPartitionsd.  TO (KoloBankPrimary, KoloBankSecondary);e.  GOf.  CREATE FUNCTION KoloBankSchemeg.  AS PARTITION SCHEMEh.  WITH PARTITION KoloBankPartitionsi.

 FOR (KoloBankPrimary, KoloBankSecondary);j.  GO

k.  CREATE OBJECT::KoloBankSchemel.  RETURNS PARTITION SCHEMEm.  FOR PARTITION KoloBankPartitionsn.  ON (KoloBankPrimary, KoloBankSecondary);o.  GOp.  CREATE PARTITION SCHEME KoloBankSchemeq.  FOR PARTITION KoloBankPartitionsr.  ON (KoloBankPrimary, KoloBankSecondary);s.  GOt.  CREATE OBJECT::KoloBankSchemeu.  AS PARTITION SCHEMEv.  ON PARTITION KoloBankPartitionsw.  TO (KoloBankPrimary, KoloBankSecondary);x.  GO

Julia is working on a large database for a commercial bank. A colleague has alreadycreated the database as follows: 

CREATE DATABASE KoloBankON PRIMARY( NAME = N'KoloBankMain',FILENAME = N'C:\Kolo Bank Primary\KoloBankMain.mdf',

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 108/132

  SIZE = 100MB,MAXSIZE = 500MB,FILEGROWTH = 10MB),

FILEGROUP KoloBankPrimary( NAME = N'KoloBankFirst',FILENAME = N'C:\Kolo Bank Primary\KoloBankFirst.ndf',SIZE = 20MB,MAXSIZE = 100MB,FILEGROWTH = 2MB),

FILEGROUP KoloBankSecondary( NAME = N'KoloBankSecond',FILENAME = N'D:\Kolo Bank Secondary\KoloBankSecondady.ndf',SIZE = 20MB,MAXSIZE = 100MB,FILEGROWTH = 2MB)

LOG ON( NAME = N'KoloBankLog',FILENAME = N'C:\Kolo Bank Logs\KoloBankLogger.ldf',SIZE = 10MB,MAXSIZE = 20MB,

FILEGROWTH = 2MB);GO

Another colleague created a partition function and a partition scheme: USE KoloBank;GOCREATE PARTITION FUNCTION KoloBankPartitions(int)AS RANGE LEFTFOR VALUES(1);GO

CREATE PARTITION SCHEME KoloBankSchemeAS PARTITION KoloBankPartitionsTO (KoloBankPrimary, KoloBankSecondary);GO

As the main database developer, Julia has to create a table that will store customers records. Among others, the table will have a column named CustomerID of type int.The above partition scheme will decide what file group must hold the records. Howshould the table be created?

.  WITH KoloBankSchemea.  CREATE TABLE Customersb.  (c.  CustomerID int identity(1, 1) primary key,d.  AccountNumber nchar(10) not null,e.  FirstName nvarchar(20),f.  LastName nvarchar(20) not nullg.  )h.  USE CustomerID AS PARTITION;i.  GOj.  CREATE TABLE Customersk.  (l.  CustomerID int identity(1, 1) primary keym.  WITH KoloBankScheme,n.  AccountNumber nchar(10) not null,o.  FirstName nvarchar(20),p.  LastName nvarchar(20) not nullq.  );

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 109/132

r.  GOs.  CREATE TABLE Customerst.  (u.  CustomerID int identity(1, 1) primary key,v.  AccountNumber nchar(10) not null,w.  FirstName nvarchar(20),x.  LastName nvarchar(20) not nully.  ) ON KoloBankScheme(CustomerID);z.  GOaa.  CREATE TABLE Customers1bb.  (cc.  CustomerID int identity(1, 1) primary key,dd.  AccountNumber nchar(10) not null,ee.  FirstName nvarchar(20),ff.  LastName nvarchar(20) not nullgg.  );hh.  SET PARTITION::KoloBankScheme FOR CustomerID;ii.  GOjj.  USING KoloBankScheme(CustomerID)kk.  CREATE TABLE Customers

ll.  (mm.  CustomerID int identity(1, 1) primary key,nn.  AccountNumber nchar(10) not null,oo.  FirstName nvarchar(20),pp.  LastName nvarchar(20) not nullqq.  );rr.  GO

Marc is creating a table to hold the employees of his organization. He has started the table as follows: 

CREATE TABLE Employees(

EmployeeNumber nchar(60),

FirstName nvarchar(20),LastName nvarchar(20),HourlySalary money,FullName AS LastName + N', ' + FirstName,

);GO

He wants the value of the FullName column to be saved in memory as an actual value. If it is possible, how should Marc modify code to make it happen?

.  The value of computed column cannot be saved

a.  CREATE TABLE Employeesb.  (c.  EmployeeNumber nchar(60),d.  FirstName nvarchar(20),e.  LastName nvarchar(20),f.  HourlySalary money,g.  FullName AS LastName + N', ' + FirstName PERSISTED,h.  );i.  GOj.  CREATE TABLE Employeesk.  (l.  EmployeeNumber nchar(60),m.  FirstName nvarchar(20),

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 110/132

n.  LastName nvarchar(20),o.  HourlySalary money,p.  PERSIST FullName AS LastName + N', ' + FirstName,q.  );r.  GOs.  CREATE TABLE Employeest.  (u.  EmployeeNumber nchar(60),v.  FirstName nvarchar(20),w.  LastName nvarchar(20),x.  HourlySalary money,y.  FullName AS LastName + N', ' + FirstName CONSTRAINT

PERSITENT,z.  );aa.  GObb.  CREATE TABLE Employeescc.  (dd.  EmployeeNumber nchar(60),ee.  FirstName nvarchar(20),ff.  LastName nvarchar(20),

gg.  HourlySalary money,hh.  FullName AS LastName + N', ' + FirstName,ii.  CONSTRAINT P_FullName PERSISTANCE(FullName)jj.  );kk.  GO

nswers

1.  Answers 

a.  Right Answer: That's the right answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

2.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer: That's the right answer

d.  Wrong Answer

e.  Wrong Answer3.  Answers 

a.  Wrong Answer: There is no sp_autogenerate stored procedure 

b.  Wrong Answer: Adding the ProductID in the INSERT statement will onlycontinue with the error

c.  Wrong Answer: Calling the MAX() aggregate function may produce the highest product number but would not lead to a solution

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 111/132

d.  Right Answer: Setting IDENTITY _INSERT to OFF will make it possible torecover from a previous IDENTITY _INSERT set to ON. The code to do this is: 

e.  SET IDENTITY_INSERT Products OFF;GO

f.  Wrong Answer: There is no AUTOINCREMENT keyword in Transact-SQL

4.  Answers 

a.  Wrong Answer: The SET operator is not used to create an index 

b.  Wrong Answer: The AS keyword will cause an error

c.  Right Answer: That's a good way to create an index 

d.  Wrong Answer: There is no selection to make when creating an index 

e.  Right Answer: The CREATE INDEX expression is not followed by EXEC

5.  Answers 

a.  Wrong Answer: CREATE PRIMARY KEY WITH is not a valid expression

b. 

Wrong Answer: That's one way to create a primary key

c.  Right Answer: That's another way to create a primary key

d.  Wrong Answer: The REFERENCES keyword is not used to create a primary key

e.  Right Answer: That's one more way to create a primary key

6.  Answers 

a.  Wrong Answer: The code will produce an error because the COUNT() functiondoesn't define what employee number it is ref erring to

b.  Right Answer: That code will produce the list of employee with the number of sales each made 

c.  Wrong Answer: The position of the GROUP BY clause will cause an error

d.  Wrong Answer: The "HAVING Amount NOT NULL" expression has no meaning

e.  Wronog Answer: The WHERE clause will cause an error

7.  Answers 

a.  Wrong Answer: That is the wrong position to include the check option clause 

b.  Wrong Answer: That is the wrong position to include the check option clause 

c.  Wrong Answer: There is no SET CHECK OPTION ON expression

d.  Right Answer: That code will make sure any new record added through the view obeys the condition

e.  Wronog Answer: That's a wrong to create a check option

8.  Answers 

a.  Wrong Answer: Yes there is something wrong with that code. It will produce an error

b.  Wrong Answer: There is no such a limit on the nchar data type 

c.  Right Answer: You can use either UNIQUE or PRIMARY KEY, but not both

d.  Wrong Answer: Yes it can, as long as you follow some rules 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 112/132

e.  Wronog Answer: That's a nice suggestion but it is not a rule 

9.  Answers 

a.  Wrong Answer: The UPDATE keyword is not used to modify a view

b.  Right Answer: That code will change the view

c.  Wrong Answer: There is no stored procedure named sp_change d.  Wrong Answer: If you want to change a view, you must indicate the complete 

list of columns 

e.  Wrong Answer: The ADD keyword does not add a new column to a view

10. Answers 

a.  Wrong Answer: ALL and the asterisk will caus an error

b.  Wrong Answer: That code will remove members who are not suspended

c.  Wrong Answer: There is no REMOVE keyword in Transact-SQL

d.  Wrong Answer: There no stored procedure named sp_remove 

e.  Right Answer: That code will delete all suspended members 

11. Answers 

a.  Wrong Answer: That code will update the records of human resources employees to change them to the personnel department

b.  Right Answer: That code will update the records of employees from the personnel department, change them to the human resources department, andshow the list of records that were changed

c.  Wrong Answer: The DELETED word will produce an error

d.  Wrong Answer: The OUTPUT clause must come after the UDATE expression

e. 

Wrong Answer: The expressions are misplaced in that formula 

12. Answers 

a.  Wrong Answer: The ItemDescription doesn't have a def ault value. Therefore, omitting it would cause an error

b.  Right Answer: 

c.  Wrong Answer: The number of fields in the INSERT code is lower than the actual number of fields in the table 

d.  Right Answer: 

13. Answers 

a.  Wrong Answer: There is no diff erence writing the INSERT INTO code before orafter the CREATE USER code 

b.  Wrong Answer: The primary key doesn't control whether records can be updated or not on a table 

c.  Right Answer: In order to be able to update records, a user must be grantedat least both SELECT and UPDATE permissions 

d.  Wrong Answer: That whole answer doesn't make sense 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 113/132

e.  Wrong Answer: In Transact-SQL, you specify whatever name you want to use when creating a user. It can even be a fictitious name 

14. Answers 

a.  Wrong Answer: The option to grant permissions is missing

b.  Wrong Answer: There is no CONNECT permission on tables 

c.  Right Answer: 

d.  Wrong Answer: GRANT OPTION = ALL is not a valid Transact-SQL statement

e.  Wrong Answer: WITH OPTION GRANT ALL is not a valid Transact-SQLstatement

15. Answers 

a.  Wrong Answer: The statement is fine and Teachers.CourseID is valid

b.  Wrong Answer: The result will include CourseID since it is in the SELECT statement

c.  Right Answer: In order to be able to update records, a user must be granted

at least both SELECT and UPDATE permissions 

d.  That answer doesn't make sense: A SELECT statement doesn't specify what a primary key is 

e.  Wrong Answer: The result will show all three fields 

16. Answers 

a.  Wrong Answer: [CMST 320] is not valid

b.  Right Answer: This will show all records of the Teachers table 

c.  Wrong Answer: The WHEN keyword is not valid in this context

d.  Right Answer: This will show all records that match the name of the teacher

e.  Right Answer: This will show all teachers whose CourseID match the criterion

17. Discussions 

a.  Wrong Answer: Either CourseID or Teachers.CourseID is valid

b.  Wrong Answer: The teachers will not appear in the result

c.  Wrong Answer: The teachers will not appear in the result

d.  Right Answer: 

e.  Wrong Answer: The other columns must not be included in the statement

18. Discusions 

a.  Wrong Answer: Only one of those fields can be used. If both are used withthe GROUP BY clause, the statement produces an error

b.  Wrong Answer: The statement produces an error because there is noaggregate function to validate the GROUP BY clause 

c.  Right Answer: This produces the list of courses and the number of teachers who teach that course 

d.  Right Answer: This produces the list of teachers and the number of courses that each teacher teaches 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 114/132

e.  Wrong Answer: The field i the SELECT section must be the same in the GROUP BY clause 

f.  Wrong Answer: The field i the SELECT section must be the same in the GROUP BY clause 

19. Discussions 

a.  Right Answer: 

b.  Wrong Answer: 

c.  Wrong Answer: 

d.  Wrong Answer: 

e.  Wrong Answer: 

20. Answers 

a.  Wrong Answer: If/Since the user is able to see the table (she didn't say thatshe could not connect to the database; she said she was denied the ability tocreate records), then she is able to connect to the database. The CONNECT 

permission is not an issue b.  Wrong Answer: If you were able to execute the script, it means there is no

problem with the username. Besides, that username is fine 

c.  Right Answer: Since the script included the permissions, you must make sure you grant the right ones. Even though you granted the INSERT permission, itis not enough. The following script should solve the problem: 

d.  USE SuperMarket1;e.  GOf.  GRANT SELECTg.  ON OBJECT::Personnel.Employeesh.  TO [Hermine];

GO

i.  Wrong Answer: That is not an issue. We assume that you are the database administrator/developer and system administrator

 j.  Wrong Answer: Who marked the table as read-only and why? That was not anissue 

21. Answers 

a.  Right Answer: A view must not include ORDER BY, unless the SELECT statement has a TOP clause 

b.  Wrong Answer: The empl name is an alias for the table and it doesn't have tobe in square brackets. Since the names of fields probably include spaces, theycan also include space in this context

c.  Wrong Answer: The names of columns can contain space is they containspace in the original table 

d.  Wrong Answer: If the ORDER BY were possible, it would be written after the FROM expression

e.  Wrong Answer: The name of a view can/must be preceded by a schema 

22. Answers 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 115/132

a.  Right Answer: You cannot create a view from a temporary table 

b.  Wrong Answer: Since you cannot create a temporary view, the name of a view cannot start with #

c.  Wrong Answer: Both SQL and Transact-SQL allow names of ob jects to containspace 

d.  Wrong Answer: The name of a temporary table must start with #

e.  Wrong Answer: That answer doesn't make sense. A primary key has nothingto do with the type of table: temporary or not

23. Answers 

a.  Wrong Answer: The result will show the list of teachers and the courses theyteach. That code is the same as: 

b.  SELECT TeacherID, FullName, Teachers.CourseIDc.  FROM Teachersd.  Wrong Answer: That code will produce an error because of the position of 

OUTER

e.  Wrong Answer: The result will show the list of teachers and the courses theyteach

f.  Wrong Answer: That code will show a list of courses: their codes and names 

g.  Right Answer: This will produce the list of all Teachers whether they have a matching CourseID record in the Courses table or not

24. Answers 

a.  Right Answer: The result is the list of all employees and the department eachemployee belongs to

b.  Wrong Answer: This would have been possible with a CROSS join, but that's not the type of join in the code 

c.  Wrong Answer: The database engine is able to recognize the relationship based on the types of fields 

d.  Wrong Answer: The code will not produce any error

e.  Wrong Answer: That answer doesn't make sense. That answer is too vague 

25. Answers 

a.  Wrong Answer: There is no error in either code 

b.  Right Answer: Both codes produce the same error

c.  Wrong Answer: There is no error in either code 

d.  Wrong Answer: There is no error in either code 

e.  Wrong Answer: Both codes are the same 

26. Answers 

a.  Wrong Answer: There is no error in the code. The WHERE expression is always written after the FROM clause 

b.  Wrong Answer: Including or omitting the Departments.DepartmentCode fielddepends on the intended result but has no eff ect on the final result

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 116/132

c.  Right Answer: The result will consist of a list of employees who belong to the Research & Development department

d.  Wrong Answer: That answer doesn't makle sense 

e.  Wrong Answer: The list will not include employees not in the RSDVdepartment

27. Answers 

a.  Wrong Answer: Yes you can delete a view

b.  Wrong advice: It appears that Maggie wants to delete a view, nothing to dowith a table 

c.  Wrong advice: There is no reason to worry about the schema 

d.  Wrong Answer: There is no reason to worry about the records. The records are stored in the table(s), not the view

e.  Right Answer: When you decide the delete a view, the main concern is aboutthe permissions because they would be lost. If you decide to re-create the view, you must grant the same permissions to the users who had access to

the old view

28. Answers 

a.  Wrong Answer: Yes triggers use or need a schema 

b.  Wrong Answer: We need neither a user nor a login. It appears that Frank justwants to tes code and chose not to specify a user or login. Besides, since the original code executes successfully, the user name or login are not an issue 

c.  Wrong Answer: That will not solve the problem 

d.  Right Answer: Both the Customers table and the ForCustomers triger mustuse the same schema 

e. 

Wrong Answer: The presence or absence of a primary key will not determine whether Frank can create a record or not. In f act, the error has nothing to do

with any of the tables in the database. The error is related to the trigger

29. Answers 

a.  Wrong Answer: That's not how the IN operator is used

b.  Right Anwer: That code will produce the list of apartments from the secondf loor

c.  Wrong Answer: There is no FROM ... TO condition in SQL

d.  Right Anwer: That code will produce the list of apartments from the secondf loor

e.  Wrong Answer: The FROM 200 TO 300 expression is invalid

30. Answers 

a.  Right Answer: That will work

b.  Wrong Answer: That code will produce an erro because of the presence of CONSTRAINT 

c.  Right Answer: That's another valid way to create a check constraint

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 117/132

d.  Wrong Answer: That code will produce an error because of ON 

e.  Wrong Answer: That code uses an invalid formula to create a check constraint

31. Answers 

a.  Wrong Answer: 20.15 is not the highest salary in the Research & Development department

b.  Wrong Answer: The value seems to come from a department diff erent thanthe one in the WHERE condition

c.  Right Answer: The code produces the highest paid salary in the Research & Development department

d.  Wrong Answer: That value is from the Accounting department

e.  Wrong Answer: That's not the highest salary in the Research & Developmentdepartment

32. Answers 

a.  Right Answer: That's the right sequence of keywords to produce a number of 

records b.  Wrong Answer: The AS clause must be included after a column

c.  Wrong Answer: The FROM keyword must not follow SELECT 

d.  Wrong Answer: When a WHERE condition is used, it must be included afterthe FROM statement

e.  Wrong Answer: The WHERE condition is not added immediately after SELECT 

33. Answers 

a.  Wrong Answer: FROM cannot follow SELECT 

b.  Wrong Answer: FROM cannot follow SELECT 

c.  Right Answer: The MAX() function is used to get the highest value of a column

d.  Wrong Answer: There is no HIGH function in Transact-SQL

e.  Wrong Answer: If used, the AS clause must be included (immediately) afterthe column

34. Answers 

a.  Wrong Answer: That code will produce the highest salary in the research & developement department

b.  Right Answer: That code will produce the highest salary in the humanresources department

c.  Wrong Answer: The presence of OUTER will produce an error in that code 

d.  Wrong Answer: There is no aggregate function named HIGH in Transact-SQL

e.  Wrong Answer: There is no function or stored procedure named sp_high

35. Answers 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 118/132

a.  Right Answer: That code will produce the list of employees and their managername, if any, in the last column. If an employee does not have a manager, the last column shows NULL

b.  Wrong Answer: A subquery can include only one column

c.  Wrong Answer: A subquery must produce only one value 

d.  Wrong Answer: That code will produce the list of employees but the lastcolumn shows the salaries 

e.  Wrong Answer: That code will produce the list of employees but the lastcolumn shows the name of the employee from the first column

36. Answers 

a.  Right Answer: That code will produce the list of employees who have a manager and will show the name of that manager on the last column

b.  Wrong Answer: That code will produce the list of employees and theirmanager's name, but the list includes the employees who don't have a manager

c.  Wrong Answer: You don't use an IS or IS NO condition after an AS alias 

d.  Wrong Answer: That code will produce the list of employees and theirmanager's employee number. The list also includes the employees who don'thave a manager

e.  Wrong Answer: The NOT NULL expression is misplaced

37. Answers 

a.  Wrong Answer: A sub-query must include only one column

b.  Right Answer: That code will produce the list of employees, their salaries, andtheir manager's name 

c. 

Wrong Answer: The parts of a WHERE statement must be on the same type.Those in empl.EmployeeNumber = sal.SalaryID are not

d.  Wrong Answer: There no SalaryID in the Employees table 

e.  Wrong Answer: Although that code will work fine, it shows the employee number twice, in the last two columns 

38. Answers 

a.  Wrong Answer: A subquery must include a condition

b.  Wrong Answer: The value provided by the subquery is a diff erent type thanthe one expected by the WHERE condition

c.  Right Answer: That code will produce the necessary information

d.  Wrong Answer: That will simply give a list of all employees and show a common salary for all of them 

e.  Wrong Answer: That code produces all employees 

39. Answers 

a.  Wrong Answer: That code will move the employees from the human resources department to the personnel department

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 119/132

b.  Right Answer: That code will move the employees from the personnel department to the human resources department

c.  Wrong Answer: The FROM keyword will produce an error

d.  Wrong Answer: The SET and the UDATE keywords are inversed

e.  Wrong Answer: The SET and the UDATE keywords are inversed

40. Answers 

a.  Wrong Answer: That code will delete the table, including all of its records 

b.  Wrong Answer: There is no REMOVE operator in Transact-SQL

c.  Wrong Answer: The asterisk is not used in a DELETE operation

d.  Right Answer: That code will delete all records from the table but keep the table itself 

e.  Wrong Answer: There is no stored procedure with that name 

41. Answers 

a.  Wrong Answer: BEGIN and END are not used when creating a viewb.  Right Answer: That code creates a view that includes the employee number, 

the first name, and the last name 

c.  Wrong Answer: The RETURN TABLE expression has no meaning here 

d.  Wrong Answer: WITH and END have no meaning here 

e.  Wrong Answer: The EXECUTE keyword is not used to create a view

42. Answers 

a.  Wrong Answer: The UPDATE keyword is not used to modify a table 

b.  Right Answer: That code will change the table and add the new column

c.  Wrong Answer: There is no EXECUTE MODIFY expresion in Transact-SQL

d.  Wrong Answer: There is stored procedure named sp_change 

e.  Wrong Answer: The AS keyword must not be used

43. Answers 

a.  Wrong Answer: The WITH keyword will create an error

b.  Right Answer: That code adds a new column that will act as a primary key

c.  Right Answer: That code adds a new column that will act as a primary key

d.  Wrong Answer: The UPDATE keyword is not used to change a table 

e.  Wrong Answer: That formula will produce an error

44. Answers 

a.  Right Answer: That code will produce the list of employees whose last names end with a 

b.  Wrong Answer: The underscore would not produce the needed result

c.  Wrong Answer: Including a in square brackets would not accomplish anything

d.  Wrong Answer: Preceding a with an accent would not do anything

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 120/132

e.  Wrong Answer: The exclamation point is not a valid wildcard in Transact-SQL

45. Answers 

a.  Wrong Answer: Considering that 0 is a value, if a product has receive 0 as its discount, then it actually has a discount

b.  Wrong Answer: That code will not produce anything

c.  Wrong Answer: That code will show the list of products whose discount is not0

d.  Wrong Answer: That code will produce an error because there is no operatorbefore NULL

e.  Right Answer: That code will show a list of products that have a discount

46. Answers 

a.  Wrong Answer: There is no such an expression as SET ENCRYPTION ON 

b.  Wrong Answer: There is no ENCRYPT WHEN DONE valid expression

c.  Right Answer: To encrypt the entry code of a view in a database, use WITH

ENCRYPTION after the name of the view

d.  Wrong Answer: The place to specify encryptio is wrong

e.  Wrong Answer: There is no such a thing as SET ENCRYPTION ON 

47. Answers 

a.  Wrong Answer: There is no reson to select anything when granting a permission

b.  Right Answer: That code will grant the ALTER permission to Peter

c.  Wrong Answer: There is no stored procedure named sp_grant

d.  Wrong Answer: There is PERMISSION keyword to be used when granting a 

permission

e.  Wrong Answer: There is no such a thing as SET PERMISSION GRANT 

48. Answers 

a.  Wrong Answer: There is no value DO SCHEMA BINDING expression inTransact-SQL

b.  Wrong Answer: The SCHEMA BINDING expression will create an error

c.  Wrong Answer: The FOR word will cause an error

d.  Right Answer: To bind a schema to its parent table, add a WITHSCHEMABINDING clause when creating the view

e.  Wrong Answer: There is no valid SET SCHEMA BINDING expression

49. Answers 

a.  Wrong Answer: The order of HAVING and GROUP BY is not right

b.  Wrong Answer: The presence of SUM will make this code show all employees 

c.  Right Answer: That code will show the employees who made sales over 4750

d.  Wrong Answer: The WHERE clause will cause that code to produce an error

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 121/132

e.  Wrong Answer: The WHERE condition creates an error

50. Answers 

a.  Right Answer: That code will show the list of employees. Each record will show the first name, the last name and the number of sales the employee made. The number of sales are ordered in ascending order

b.  Wrong Answer: Since the Amount is not represented in the SELECT statement, the code will produce an error

c.  Wrong Answer: The presences of ORDER BY, HAVING, and ORDER BY as misplaced

d.  Wrong Answer: The FROM expression must come immediately after the SELECT statement

e.  Wrong Answer: The GROUP BY expression must be the last

51. Answers 

a.  Wrong Answer: There is no valid RENAME COLUMN expression in Transact-SQL

b.  Wrong Answer: There is no RENAME operator to rename a column

c.  Right Answer: You use the sp_rename stored procedure to rename a columne and you should add N'COLUMN'

d.  Wrong Answer: There in stored procedure named sp_changename 

e.  Wrong Answer: There is no RENAME operator to rename a column

52. Answers 

a.  Wrong Answer: The DELETE keyword is not used to remove a column

b.  Wrong Answer: The SET keyword is not used to remove a column

c. 

Wrong Answer: The UPDA

TE keyword is not used to remove a column

d.  Right Answer: That code will delete the column

e.  Wrong Answer: The UPDATE keyword is not used to remove a column and the SET keyword cannot be used as in this code 

53. Answers 

a.  Right Answer: That code will show the records from the Employees table.Each record will show each of the records from the second table. The resultwill include only products that cost more than 50.00

b.  Wrong Answer: There is no reason to use DISTINCT here 

c.  Wrong Answer: You cannot create a Boolean operation between a string-

based column ([Empl #] and a number-based column (Number)

d.  Wrong Answer: You can't use GROUP BY since there is no aggregate functionin the statement

e.  Wrong Answer: Since the unit price is not used in an aggregate function inthe SELECT statement, this code will produce an error

54. Answers 

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 122/132

a.  Wrong Answer: Unlike the primary key that can be created separate from its column, a foreign key must be created in the statement of its column

b.  Right Answer: That code will create a foreign key that ref erences a columnfrom the Employees table 

c.  Right Answer: That's another way to create a primary key

d.  Wrong Answer: If you decide to use the CONSTRAINT keyword, you mustcreate a name for the foreign key

e.  Wrong Answer: CREATE FOREIGN KEY WITH is not a valid expression

55. Answers 

a.  Wrong Answer: You don't specify the name of the column when deleting anindex 

b.  Wrong Answer: The DELETE keyword is not valid in this context

c.  Wrong Answer: The FROM keyword will cause an error

d.  Right Answer: That's the code to delete an index 

e.  Wrong Answer: The DELETE keyword cannot be used like that

56. Answers 

a.  Wrong Answer: The combination of word after ALTER TABLE is not valid

b.  Wrong Answer: You don't use UPDATE to add a new column

c.  Wrong Answer:The AS keyword is not used when adding a new column

d.  Wrong Answer: There is no CHANGE keyword to be used here 

e.  Right Answer: That code will add a new column that is a foreign key

57. Answers 

a. 

Wrong Answerb.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Right Answer: That's the right formula 

58. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer: That's the right formula 

d.  Wrong Answer

e.  Wrong Answer

59. Answers 

a.  Right Answer: That's the right answer

b.  Wrong Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 123/132

d.  Wrong Answer

e.  Wrong Answer

60. Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Wrong Answer

d.  Right Answer: That's the right answer

e.  Wrong Answer

61. Answers 

a.  Wrong Answer

b.  Right Answer: That's the right answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

62. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer: That's the right answer

d.  Wrong Answer

e.  Wrong Answer

63. Answers 

a.  Wrong Answer: There is no AUTONUMBER in Transact-SQL

b.  Wrong Answer: The COUNTER word will produce an error

c.  Wrong Answer: There is no AUTOINCREMENT in Transact-SQL

d.  Right Answer: That's the right answer

e.  Wrong Answer

64. Answers 

a.  Right Answer: That's the right answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

65. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 124/132

d.  Right Answer: That's the right answer

e.  Wrong Answer

66. Answers 

a.  Wrong Answer

b.  Right Answer: That's the right answerc.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

67. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Right Answer: That's the right answer

68. Answers 

a.  Right Answer: That's the right answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

69. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer: That's the right answer

d.  Wrong Answer

e.  Wrong Answer

70. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Right Answer: That's the right answer

e.  Wrong Answer

71. Answers 

a.  Wrong Answer

b.  Right Answer: That's the right answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 125/132

d.  Wrong Answer

e.  Wrong Answer

72. Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Wrong Answer

d.  Wrong Answer

e.  Right Answer: That's the right answer

73. Answers 

a.  Right Answer: That's the right answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

74. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

75. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

76. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

77. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 126/132

d.  Wrong Answer

e.  Wrong Answer

78. Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

79. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

80. Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

81. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Right Answer

82. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

83. Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 127/132

d.  Wrong Answer

e.  Wrong Answer

84. Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

85. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

86. Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

87. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

88. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Right Answer

89. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 128/132

d.  Wrong Answer

e.  Wrong Answer

90. Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

91. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

92. Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

93. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

94. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

95. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 129/132

d.  Wrong Answer

e.  Right Answer

96. Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

97. Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

98. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

99. Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

100.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Right Answer

101.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 130/132

d.  Wrong Answer

e.  Wrong Answer

102.  Answers 

a.  Wrong Answer

b.  Right Answerc.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

103.  Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

104.  Answers 

a.  Wrong Answer

b.  Right Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

105.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

106.  Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

107.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 131/132

d.  Wrong Answer

e.  Wrong Answer

108.  Answers 

a.  Right Answer

b.  Wrong Answerc.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

109.  Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

110.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

111.  Answers 

a.  Wrong Answer

b.  Right Answer

c.  Wrong Answer

d.  Wrong Answer

e.  Wrong Answer

112.  Answers 

a.  Wrong Answer

b.  Wrong Answer

c.  Wrong Answer

d.  Right Answer

e.  Wrong Answer

113.  Answers 

a.  Right Answer

b.  Wrong Answer

c.  Wrong Answer

8/6/2019 SQL Quries

http://slidepdf.com/reader/full/sql-quries 132/132

d.  Wrong Answer

e.  Wrong Answer

114.  Answers 

a.  Wrong Answer

b.  Wrong Answerc.  Right Answer

d.  Wrong Answer

e.  Wrong Answer

115.  Answers 

a.  Wrong Answer: Yes, it is possible to persist the value of a computed column

b.  Right Answer: The ask the database engine to store the value of a computedcolumn, you must add the PERSISTED keyword

c.  Wrong Answer: There is no PERSIST keyword in Transact-SQL

d.  Wrong Answer: Persistence is not created as a constraint

e.  Wrong Answer: Persistence is not created as a constraint

116. 

Previous Copyright © 2009-2011 FunctionX.com Next