Database Lab Manual 4 Data Retrieval & Set Operations FAST NUCES
Database Lab Manual 4 Data Retrieval & Set Operations FAST NUCES
Sciences
Lab Manual 4
“Data Retrieval & Set Operations”
Database Systems
SPRING 2023
Table of Contents
1. Objective................................................................................................................................................................2
2. Pre-requisites..........................................................................................................................................................2
3. SELECT-FROM-WHERE.....................................................................................................................................3
Most Basic Select:......................................................................................................................................................3
Retrieving Certain Columns from Select...................................................................................................................4
Retrieving Certain Rows from SELECT - WHERE CLAUSE..................................................................................4
Like Operator Scenarios.............................................................................................................................................5
Renaming Resulting Column.....................................................................................................................................5
SQL Server Built-in Functions...................................................................................................................................5
4. Order by Clause.....................................................................................................................................................6
TOP Clause.................................................................................................................................................................6
5. Arithmetic Operations............................................................................................................................................7
6. Set Operations........................................................................................................................................................8
Page 1
FAST NU Lahore Database Systems CL 218
1. Objective
The purpose of this manual is to get stared with data retrieval queries, starting from simple Select-From-Where,
Order by clause, arithmetic operations and finally covering set operations.
2. Pre-requisites
Lab 2 manual, on how to get started with MS-SQL server
How Select-From-Where clause works
How Order by clause works
How arithmetic operations like +, -, *, /, % works
How set Operations like Union, Intersect, Except work
Task Distribution
Total Time 170 Minutes
Select-From-Where 25 Minutes
Order By 10 Minutes
Arithmetic Operations 10 Minutes
Set Operations 15 Minutes
Exercise 90 Minutes
Evaluation Last 20 Minutes
Page 2
FAST NU Lahore Database Systems CL 218
3. SELECT-FROM-WHERE
Select from where is equivalent to projection and selection in Relational Algebra, it will give output in form of a
table.
The most basic select statement includes Select and from clause, and it will retrieve all columns and rows from the
table.
We will use the following schema and database for the examples. Find the queries for this database in
InLab3Practice.sql and start practicing.
Syntax:
SELECT *
FROM <tableName>
Try this
Results
Page 3
FAST NU Lahore Database Systems CL 218
Syntax:
SELECT ColumnX, ColumnY, ColumnZ
FROM <tableName>
Try this
Results
Syntax:
SELECT *
FROM <tableName>
where <conditions>
Try this
Results
Page 4
FAST NU Lahore Database Systems CL 218
Syntax:
SELECT ColumnX as X , ColumnY as Y, ColumnZ
FROM <tableName> as Table1
Try this
Results
Syntax:
1) SELECT GETDATE();
3) SELECT CURRENT_TIMESTAMP;
2) SELECT SUBSTRING(columnName, startposition, substringlength) AS alias FROM <tableName>;
Page 5
FAST NU Lahore Database Systems CL 218
Try to explore as many string and data functions through this link:
https://www.w3schools.com/sql/sql_ref_sqlserver.asp
4. Order by Clause
Order by clause is used to arrange the rows in ascending or descending order of one or more columns
Syntax:
SELECT ColumnX as X, ColumnY as Y, ColumnZ
FROM <tableName> as Table1
ORDER BY ColumnX asc/desc, ColumnZ asc/desc
Try this
Results
TOP Clause
Top n clause will give you first n rows from result instead of all the rows.
Syntax:
SELECT TOP <n> *
FROM <tableName>
WHERE <conditions>
ORDER BY <column Name> asc/desc
Try this
Page 6
FAST NU Lahore Database Systems CL 218
5. Arithmetic Operations
Sql arithmetic operators are:
+ Addition
- Subtraction
/ Division
* Multiplication
% Modulus
Syntax:
1. Apply operation on single columns
SELECT ColumnX, ColumnY + 100
FROM <tableName>
Page 7
FAST NU Lahore Database Systems CL 218
6. Set Operations
Result of two (or more) select queries can be combined using set operations such as UNION, INTESECT, EXCEPT.
Syntax:
SELECT ColumnX, ColumnY
FROM <tableName>
Union/Intersect/Except
NOTE: The output of first select query should have same number and type of column as of second select query.
Page 8
FAST NU Lahore Database Systems CL 218
7. Join Operation
Inner Join:
Returns only those rows that match in both tables.
SELECT *
FROM <table1> inner join <table2>
ON <Joining Condition>
Page 9