Advanced SQL- LAB 1
Subqueries,
DCL and TCL
Statements
Variables,
Selection, and
Iteration
Stored
Procedures
Tables and
Functions
Error
Handling
Using DDL Statements The Knowledge Academy
Advanced SQL – LAB 1
1. Overview.............................................................................................................................................................. 2
2. The Lab Contents ................................................................................................................................................. 2
3. Subqueries ........................................................................................................................................................... 3
3.1 The ALL Operator ............................................................................................................................................... 3
3.2 The IN Operator ................................................................................................................................................. 4
3.3 The ANY Operator .............................................................................................................................................. 4
3.4 Correlated Subquery .......................................................................................................................................... 5
3.4.1 Updating with Co-Related Subquery........................................................................................................... 5
3.4.2 Deleting with Co-Related Subquery ............................................................................................................ 6
3.5 Using EXISTS ....................................................................................................................................................... 6
3.5.1 Simple Case of EXISTS ................................................................................................................................. 6
3.5.2 Using NOT EXISTS ........................................................................................................................................ 7
3.5.3 Inserting with EXISTS................................................................................................................................... 7
3.5.4 Deleting with EXISTS ................................................................................................................................... 7
4. Data Control Language (DCL) .............................................................................................................................. 8
4.1 Granting Permissions ......................................................................................................................................... 8
4.2 Revoking Permissions......................................................................................................................................... 8
5. Transaction Control Language (TCL) ................................................................................................................... 9
5.1 Rolling Back Transactions .................................................................................................................................10
5.2 Committing Transactions ................................................................................................................................... 9
5.3 SAVING TRANSACTIONS ...................................................................................................................................10
5.4 SET TRANSACTION ...........................................................................................................................................11
Page | 1
Using DDL Statements The Knowledge Academy
1. Overview
The Advanced SQL course starts with an in-depth view of Subqueries, delves further
into Programmability, and completes with Error Handling and Debugging. It comprises
5 Labs –
Lab 1) Subqueries, DCL and TCL statements
Lab 2) Variables, Selection, and Iteration
Lab 3) Stored Procedures
Lab 4) Functions
Lab 5) Error Handling & Debugging.
2. The Lab Contents
Tasks
Subqueries
DCL Statements
TCL Statements
Page | 2
Using DDL Statements The Knowledge Academy
3. Subqueries
A subquery is a query inside a query. The inside query can return a single or multiple
values. As such, Subqueries use three operators – ALL, IN, and ANY.
3.1 The ALL Operator
The ALL operator returns a true value when the comparison being made is true for all
the values.
Find the name of the Customer who is not placing an Order.
To perform the above query follow the steps below:
From the Standard Toolbar , click the New Query Option
In the Query Window, issue the following statement
Select CustomerName as [Customer Not Placing Orders]
From Customer
Where CustomerId <> All (Select CustId from Orders)
Select the Execute option from the Standard Toolbar
Figure 1. Using the ALL Operator with Subqueries
Another example of ALL operator with HAVING statement
Find the maximum ordered product
To perform the above query follow the steps below:
From the Standard Toolbar , click the New Query Option
In the Query Window, issue the following statement
SELECT PRODUCTNAME FROM PRODUCT
WHERE PRODUCTID = ALL (SELECT PRODUCTID FROM ORDER_DETAILS GROUP
BY PRODUCTID HAVING COUNT (*) > 1)
Page | 3
Using DDL Statements The Knowledge Academy
Select the Execute option from the Standard Toolbar
Figure 2.Using the ALL Operator with HAVING Statement
3.2 The IN Operator
The IN operator is used with Subqueries as a substitute for OR. If one value matches the
compared column IN returns a true.
Find the CustomerName by using the Order table
To perform the above query follow the steps below:
From the Standard Toolbar , click the New Query Option
In the Query Window, issue the following statement
SELECT CustomerName from Customer
Where CustomerId IN (Select CustId from Orders)
Select the Execute option from the Standard Toolbar
Figure 3. Using the IN Operator
3.3 The ANY Operator
The ANY Operator also serves the same purpose as that of IN. The difference is ANY uses
comparison operators <, =,> for comparison, as will become clear.
Find the CustomerName by using the Order table
To perform the above query follow the steps below:
From the Standard Toolbar , click the New Query Option
In the Query Window, issue the following statement
SELECT CustomerName from Customer
Where CustomerId = ANY (Select CustId from Orders)
Page | 4
Using DDL Statements The Knowledge Academy
Select the Execute option from the Standard Toolbar
Figure 4. Using the ANY Operator
3.4 Correlated Subquery
A correlated subquery uses data from the parent / outer query. Thus the subquery is
dependent on the parent query. The parent query can be SELECT, UPDATE or DELETE
STATEMENT
Find the CustomerId and CustomerName whose product is shipped by the same supplier
To perform the above query follow the steps below:
• From the Standard Toolbar , click the New Query Option
• In the Query Window, issue the following statement
SELECT Customerid, Customername
FROM Customer
WHERE Customerid IN (SELECT CustId
FROM Orders where SupplierId=8374)
• Select the Execute option from the Standard Toolbar
Figure 5.The Co-related Subquery
3.4.1 Updating with Co-Related Subquery
Update the Customer table
Update Customer
Set City = 'Johannesburg', Country = 'South Africa', PostalCode = 2000
WHERE Customerid IN (SELECT CustId FROM Orders where SupplierId=8374)
Page | 5
Using DDL Statements The Knowledge Academy
Figure 6. Using Update with Co-Related Subquery
3.4.2 Deleting with Co-Related Subquery
A Co-related subquery can also be used for deleting records much like the update
statement
Delete from Customer
WHERE Customerid NOT IN (SELECT CustId FROM Orders)
Figure 7. Deleting with Co-Related Subquery
3.5 Using EXISTS
3.5.1 Simple Case of EXISTS
EXISTS is used in a subquery to check if the inner query returns any record or not. Only
when the EXISTS returns true, the outer or the main query will execute.
Display the name of those customers who have placed orders.
Select Customername from Customer C where
Exists (Select Custid from Orders where C.CustomerId = Orders. CustId)
Figure 8. Using EXISTS with a Subquery
Page | 6
Using DDL Statements The Knowledge Academy
3.5.2 Using NOT EXISTS
The reverse of EXISTS is NOT EXISTS. The outer query will execute if the inner query is
not satisfied.
Display the name of those customers who have not placed any orders.
Select Customername from Customer C where NOT
Exists (Select Custid from Orders where C.CustomerId = Orders. CustId)
Figure 9. Using NOT EXISTS
3.5.3 Inserting with EXISTS
Insert the CustomerId, and OrderDate of the customers who have placed orders into
the FAVCUSTOMER table
INSERT FavCustomer
Select CustId, OrderDate from Orders where Exists
(Select CustId from Orders INNER JOIN Customer
ON Customer. Customerid = Orders. CustId)
Figure 10. Using EXISTS with INSERT
3.5.4 Deleting with EXISTS
Delete those records from the FAVCUSTOMER Table which exists in the Order Table
Delete from FavCustomer
Where Exists
(Select CustId from Orders INNER JOIN Customer
Page | 7
Using DDL Statements The Knowledge Academy
ON Customer. Customerid = Orders. CustId)
Figure 11. Using DELETE with EXISTS
4. Data Control Language (DCL)
Data Control Language comprises of just two statements – GRANT and REVOKE. While
the previous one is required to give permission, the latter one takes the permissions
back.
4.1 Granting Permissions
Give the labuser11 user permission to insert, update, and view records from
orders table
While logged in as the ‘Super User’ issue the following statement
Grant Select, Insert, Update on CustomerOrders. [dbo].Orders to labuser11
Figure 12. Granting Privileges
4.2 Revoking Permissions
Revoke the permission to delete records from the order table from labuser11
While logged in as the ‘Super User’ issue the following statement
Revoke on Orders from labuser11
Login as labuser11 and issue the following statements
Use CustomerOrders
Go
Delete from Orders where OrderId=1011
Page | 8
Using DDL Statements The Knowledge Academy
Figure 13. REVOKING PRIVILIGES
5. Transaction Control Language (TCL)
Transaction Control Language or TCL Statements including the commands to Save data
or Rollback data as an atomic unit so as to maintain the Database Integrity. A transaction
begins with a BEGIN TRAN statement and may end with a ROLLBACK TRAN or COMIT
TRAN. It can also use the SAVE command to roll back to a certain point when required.
5.1 Committing Transactions
A COMMIT means that data is successfully saved to the database. In case of
ROLLBACK, the data will not be lost.
Begin Tran
Insert into Orders Values (1014, 5604,'12-Dec-2017', 8374)
Select * from Orders
Commit Tran
Figure 14. Committing Transactions
Page | 9
Using DDL Statements The Knowledge Academy
5.2 Rolling Back Transactions
Begin Tran
Insert into Orders Values (1014, 5611,'12-Dec-2017', 8374)
Insert into Orders Values (1016, 5611,'12-Dec-2017', 8374)
Select * from Orders
Rollback Tran
Figure 15. Using Rollback
Both the records are not inserted in this case as the two inserts are a part of a single
transaction
5.3 SAVING TRANSACTIONS
Sometimes a user may need to just rollback transactions up to a certain point. This is
where SAVE TRAN is useful. An example follows below
Begin Tran
Insert into Orders Values (1014, 5604,'12-Dec-2017', 8374)
Save Tran First
Insert into Orders Values (1015, 5601,'12-Dec-2017', 8374)
Save Tran Second
Insert into Orders Values (1016, 5602,'12-Dec-2017', 9374)
Save Tran Third
Select * from Orders
Rollback Tran Second
Page | 10
Using DDL Statements The Knowledge Academy
Figure 16. Understanding SAVE TRAN
5.4 SET TRANSACTION
SET TRANSACTION is used for specifying the characteristics of a transaction such
as making it read only.
Set Transaction ISOLATION LEVEL REPEATABLE Read
Begin Tran
Insert into Orders Values (1020, 5602,'12-Dec-2017', 9374)
Select * from Orders
Commit Tran
===================== End of Lab Session 1 =======================
Page | 11