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

Week3v2 (2)

The document is a lab manual focused on SQL SELECT queries, detailing their structure, usage, and capabilities, including filtering and sorting data. It outlines learning objectives, provides examples of SQL syntax, and includes practice tasks for students to apply their knowledge. Additionally, it discusses operator precedence and null value handling in SQL queries.

Uploaded by

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

Week3v2 (2)

The document is a lab manual focused on SQL SELECT queries, detailing their structure, usage, and capabilities, including filtering and sorting data. It outlines learning objectives, provides examples of SQL syntax, and includes practice tasks for students to apply their knowledge. Additionally, it discusses operator precedence and null value handling in SQL queries.

Uploaded by

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

Database Systems

Lab Manual 3

Learning Objectives:
 Understand the structure and use of basic SELECT queries.
 Retrieving and Restricting data using the SQL SELECT
statement.
 Apply SQL clauses to filter, sort, group, and manipulate
data for efficient analysis.
 Identify the impact of operator precedence and null value
handling in SQL queries.

Note: You cannot use any SQL clause other than SELECT, FROM,
WHERE, ORDER BY. FROM clause can include one table only. All tasks
will be performed on northwind schema.

LO1: Understand the structure and use of basic SELECT


queries
Capabilities of SQL SELECT Statements
The SELECT statement is one of the most fundamental and
frequently used commands in SQL. It is used to retrieve data from one
or more tables in a database. With a SELECT statement you can use
the following capabilities:
• Projection: Select the columns in a table that are returned by a
query. Select as few or as many of the columns as required.
• Selection: Select the rows in a table that are returned by a
query. Various criteria can be used to restrict the rows that are
retrieved.
• Joining: Bring together data that is stored in different tables by
specifying the link between them. SQL joins are covered in
more detail in the next labs.

Database Systems-Manual 3 Page | 1


Structure of Basic SELECT Statement

Figure 1:Structure Of Select Statement

In its simplest form, a SELECT statement must include the follow in:
• A SELECT clause, which specifies the columns to be - in this
case, the Company Name field.
• A FROM clause, which identifies the table containing the
columns that are listed in the SELECT statement.

Basic Syntax:
SELECT column1, column2, ...
FROM table_name;

WHERE Clause:
The WHERE clause filters rows based on specified conditions.
SELECT column1, column2
FROM table_name
WHERE condition;

Database Systems-Manual 3 Page | 2


Throughout this course, the words keyword, clause: and statement
are used as follows:
• A keyword is a predefined word in SQL that has a specific
meaning and function. Keywords are reserved words that are
essential to creating valid SQL queries. For example, SELECT
and FROM are keywords.
• A clause is a specific part of a SQL statement that performs a
function. A clause can include one or more keywords, and it
typically addresses a specific task, such as selecting data,
filtering records, or sorting results. For example, SELECT
name, cnic and so on is a clause.
• A statement in SQL is a complete SQL command that can
perform an action or return a result. A statement is often made
up of multiple clauses. A statement could be as simple as
retrieving data from a table or as complex as updating or
deleting records based on certain conditions.
Note:
• SQL statements are not case-sensitive.
• SQL statements can be entered on one or more lines.
• Keywords cannot be abbreviated or split across lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.
• SQL statements can optionally be terminated by a semicolon
(;). Semicolons are required when you execute multiple SQL
statements.
Comparison and Logical Operators:
Comparison operators are used to compare two values in MySQL. They
are often used in the WHERE clause of SQL statements. For example =,
<>, >, >=, <=, <, IS NULL, IS NOT NULL, BETWEEN, IN, LIKE.
SELECT column1, column2
FROM table_name
WHERE column_name [Comparison Operator] value;

Database Systems-Manual 3 Page | 3


Logical operators are used to combine multiple conditions in a query. For
example AND, OR, NOT.
SELECT column1, column2
FROM table_name
WHERE condition1 [Logical operators]condition2;
Format of Files Submission:
Write all the answers in a notepad file. Each Query should start with
Q1, Q2 depending on Question No. After that you will write your query
in one line, after that there should be three * and then there should be
two blank lines. All SQL files should be formatted as below all over
this semester.
Q1

SELECT * FROM Table1

***

Q2

SELECT col1 from Table3 WHERE col2=5

***

Figure 2:Format of File Submission

LO2: Retrieving and Restricting data using the SQL


SELECT statement.

To understand the concepts of retrieving and restricting data, let’s start


with a base table. We will then explore each scenario with examples.

Create Table
CREATE TABLE Employees (
EmployeeId INT PRIMARY KEY,
FullName VARCHAR(50),
Department VARCHAR(50),

Database Systems-Manual 3 Page | 4


Salary INT
);
Insert Data
INSERT INTO Employees (EmployeeId, FullName, Department,
Salary)
VALUES
(1, 'A', 'IT', 50000),
(2, 'B', 'HR', 60000),
(3, 'C', 'IT', 45000);
Retrieve All Data
Query:
SELECT * FROM Employees;
Output:
Employee Id FullName Department Salary
1 A IT 50000
2 B HR 60000
3 C IT 45000
Retrieve Specific Columns
Query:
SELECT FullName, Department
FROM Employees;
Output:
FullName Department
A IT
B HR
C IT
Filter Rows Using WHERE
Query:
SELECT FullName,Department

Database Systems-Manual 3 Page | 5


FROM Employees
WHERE Department = 'IT';
Output:
FullName Department
A IT
C IT

Practice Task:
1. List the names of products which priced within range 25 to 98
(ProductName).
2. Which employees are fluent in French?(FullName)
3. List employees who have completed a degree in psychology(
FirstName, LastName).
4. List the customers who are used to live in London
(CustomerName,Country).
5. List all orders where the EmployeeID is assigned (OrderID,
EmployeeID, ShipperID).
6. Retrieve suppliers with a SupplierID between 5 and 15
(SupplierName, SupplierID).
7. Retrieve products where the product contains the word
"bottles"(ProductName).
8. Retrieve products where the ProductName starts with the letter "C"(
ProductName ,Price ).
9. List the products which are shipped in boxes. (ProductName)
10. List the cities of Germany from customer table.(City)
Submission Requirements:
Submit the following files:
 P2Task.txt

Database Systems-Manual 3 Page | 6


LO3: Apply SQL clauses to filter, sort, group, and
manipulate data for efficient analysis.
DISTINCT Clause
SELECT DISTINCT column_name
FROM table_name;
Query:
SELECT DISTINCT Department
FROM Employees;
Output:
Department
IT
HR
Filter Rows with Multiple Conditions
SELECT column_name
FROM table_name
ORDER BY column_name ASC;
Query:
SELECT FullName
FROM Employees
Order By FullName ASC;
Output:
FullName
A
B
C

Filter Rows with NULL Values


SELECT column_name
FROM table_name
WHERE column_name IS NULL;

Database Systems-Manual 3 Page | 7


Concatenate Columns
SELECT CONCAT(column1, ' ', column2) AS alias_name
FROM table_name;
Practice Tasks:
1. Our customers belong to how many unique countries(CountryList).
2. Find employees whose names contain the letter "A" but not at the
start (FirstName, LastName).
3. Retrieve the suppliers sorted by their SupplierName in ascending
order (SupplierName).
4. Retrieve shippers sorted alphabetically by name(ShipperName).
5. Retrieve all customers whose address contains 'Str.(CustomerName,
Address).
6. Find products where the quantity mentions "kg" or "g"(
ProductName, Unit).
Submission Requirements:
Submit the following files:
 P3Task.txt

LO4: Identify the impact of operator precedence and null


value handling in SQL queries.

Table 1: Operators Precedence

Operator Type Example Precedence


Unary operators NOT, !, - 1
Multiplication, division, *, /, %, DIV 2
modulo
Addition, subtraction +, - 3
Comparison operators =, <>, !=, <, <=, >, >= 4
Logical NOT NOT 5
Logical AND AND 6
Logical OR OR 7
Database Systems-Manual 3 Page | 8
Practice Tasks:
Figure 3: Execution Order of SQL Statement

1. Identify at least one SQL statement in which precedence can


affect the result of query.
2. Identify how the result of a mathematical expression on null value
affect the result of a query.
Submission Requirements:
Submit the following files:
 P4Task.docx

HackerRank Tasks:
1. Japanese Cities' Attributes
2. Employee Salaries
3. Select All
4. Japanese Cities' Attributes
5. Employee Names
6. Select By ID
7. Weather Observation Station 1
8. Weather Observation Station 3
9. Weather Observation Station 4
10. Weather Observation Station 5

Database Systems-Manual 3 Page | 9


11. Higher Than 75 Marks
12. Revising the Select Query I

Submission Requirements On Eduko


1. Zip all the files of your tasks zip file name must be your
registration number. e.g. 2024-CS-X:
a. LO2
b. LO3
c. LO4

Database Systems-Manual 3 Page | 10

You might also like