0% found this document useful (0 votes)
81 views

Database Lab Manual 4 Data Retrieval & Set Operations FAST NUCES

This document provides a lab manual on data retrieval queries in SQL, including SELECT-FROM-WHERE clauses, ORDER BY clauses, arithmetic operations, and set operations. It includes examples of selecting certain columns and rows, renaming columns with aliases, built-in functions, ordering results, limiting results with TOP, and combining result sets with UNION, INTERSECT, and EXCEPT. The lab manual is intended to get students started with basic to more advanced SQL queries using a sample database schema.

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Database Lab Manual 4 Data Retrieval & Set Operations FAST NUCES

This document provides a lab manual on data retrieval queries in SQL, including SELECT-FROM-WHERE clauses, ORDER BY clauses, arithmetic operations, and set operations. It includes examples of selecting certain columns and rows, renaming columns with aliases, built-in functions, ordering results, limiting results with TOP, and combining result sets with UNION, INTERSECT, and EXCEPT. The lab manual is intended to get students started with basic to more advanced SQL queries using a sample database schema.

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

National University of Computer and Emerging

Sciences

Lab Manual 4
“Data Retrieval & Set Operations”

Database Systems
SPRING 2023

Department of Computer Science


FAST-NU, Lahore, Pakistan
FAST NU Lahore Database Systems CL 218

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.

Most Basic Select:


Retrieve data from table. Operator * after select means that all columns will be retrieved.

Syntax:
SELECT *
FROM <tableName>

Try this

Results

Page 3
FAST NU Lahore Database Systems CL 218

Retrieving Certain Columns from Select


To retrieve only certain columns give a comma separated list of those columns after Select keyword

Syntax:
SELECT ColumnX, ColumnY, ColumnZ
FROM <tableName>

Try this

Results

Retrieving Certain Rows from SELECT - WHERE CLAUSE


Rows can be filtered in SQL using WHERE clause. Rows that fulfill where clause conditions will be projected in
result. Where clause can put condition on original columns of tables mentioned in from clause. Also, observe the use
of Like operator in where clause.

Syntax:
SELECT *
FROM <tableName>
where <conditions>

Try this

Results

Page 4
FAST NU Lahore Database Systems CL 218

Like Operator Scenarios


WHERE CourseName LIKE 'C%' Finds any values that start with "C"
WHERE CourseName LIKE '%C' Finds any values that end with "C"
WHERE CourseName LIKE '%Co%' Finds any values that have "Co" in any position
WHERE CourseName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CourseName LIKE 'C_%' Finds any values that start with "C" and are at least 2
characters in length
WHERE CourseName LIKE 'C__% Finds any values that start with "C" and are at least 3
characters in length
WHERE CourseName LIKE 'C%r' Finds any values that start with "C" and ends with "r"

NOTE: % is referred to as wildcard.

Renaming Resulting Column


You can rename a column in result by using AS keyword also called Alias. The scope of this renaming is only to
that select query, this is useful in joining where more than one table have same column names.

Syntax:
SELECT ColumnX as X , ColumnY as Y, ColumnZ
FROM <tableName> as Table1

Try this

Results

SQL Server Built-in Functions


Sql Server has many built-in functions which can be used for different purposes.
For example:
1) GETDATE Returns the current database system date and time
2) CURRENT_TIMESTAMP Returns the current date and time
3) SUBSTRING Extracts some characters from a string

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

All operations can be performed on either single column or multiple columns

Syntax:
1. Apply operation on single columns
SELECT ColumnX, ColumnY + 100
FROM <tableName>

2. Apply operation on multiple columns


SELECT ColumnX, ColumnY + ColumnZ
FROM <tableName>

Replace + with other operators and try them out yourself.

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

SELECT ColumnX, ColumnY


FROM <tableName>

NOTE: The output of first select query should have same number and type of column as of second select query.

Try this –Set operations

Try this - error to look out for in set operations

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

You might also like