0% found this document useful (0 votes)
50 views7 pages

DB Lab4

This document is a laboratory manual for a database systems course. It provides an introduction to structured query language (SQL) with objectives to cover basic SQL queries, data retrieval, operators, and more. The document gives examples of SQL statements using SELECT, WHERE, AND, OR and NOT to query a sample Customers database table. It explains how to retrieve data, use operators to filter records, combine conditions, and select specific fields in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

DB Lab4

This document is a laboratory manual for a database systems course. It provides an introduction to structured query language (SQL) with objectives to cover basic SQL queries, data retrieval, operators, and more. The document gives examples of SQL statements using SELECT, WHERE, AND, OR and NOT to query a sample Customers database table. It explains how to retrieve data, use operators to filter records, combine conditions, and select specific fields in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

University of Management and Technology

Knowledge Unit of Systems and Technology

Laboratory Manual

[CC2141L]: [Database Systems]

[Semester Spring-2023]

Lab [4]: [Basics of structured Query Language (SQL)]

Lab Instructor: [Jannat-ul-mava]

[CC2141L]: [Database Systems]

Page 1
University of Management and Technology

Basics of structured Query Language (SQL)


The objectives of this lab are:

 Introduction to SQL
 Basic Query Structure
 Query Processing 
 Data retrieval Language
 Calculated fields and use of AS keyword
 SQL AND, OR and NOT Operators

 Introduction to SQL

SQL is a standard language for storing, manipulating and retrieving data in databases. SQL can do
the following below task:

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views

 Basic Query Structure

SQL Statements
Most of the actions you need to perform on a database are done with SQL statements. The
following SQL statement selects all the records in the "Customers" table:

Example
SELECT * FROM Customers;

[CC2141L]: [Database Systems]

Page 2
University of Management and Technology

Keep in Mind That...

 SQL keywords are NOT case sensitive: select is the same as SELECT

Semicolon after SQL Statements?

Some database systems require a semicolon at the end of each SQL statement. Semicolon is the
standard way to separate each SQL statement in database systems that allow more than one SQL
statement to be executed in the same call to the server.

 Query Processing 

Query Processing is the activity performed in extracting data from the database. In query
processing, it takes various steps for fetching the data from the database. The steps involved are:

1. Parsing and translation


2. Optimization
3. Evaluation

 Data retrieval Language

The SQL SELECT Statement

The SELECT statement is used to select data from a database. The data returned is stored in a result
table, called the result-set.

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

Or
SELECT * FROM table_name;

SELECT DISTINCT Examples

[CC2141L]: [Database Systems]

Page 3
University of Management and Technology

The following SQL statement selects only the DISTINCT values from the "Country" column in the
"Customers" table:

Example: SELECT DISTINCT Country FROM Customers;

WHERE SQL Statement

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a
specified condition.

WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Text Fields vs. Numeric Fields

SQL requires single quotes around text values (most database systems will also allow double
quotes). However, numeric fields should not be enclosed in quotes:

Example: SELECT * FROM Customers
WHERE CustomerID=1;

Operators in The WHERE Clause

The following operators can be used in the WHERE clause:

Operator Description Example

SELECT * FROM Products


= Equal
WHERE Price = 18;

> Greater than SELECT * FROM Products

[CC2141L]: [Database Systems]

Page 4
University of Management and Technology

WHERE Price > 30;

SELECT * FROM Products


< Less than
WHERE Price < 30;

SELECT * FROM Products


>= Greater than or equal
WHERE Price >= 30;

SELECT * FROM Products


<= Less than or equal
WHERE Price <= 30;

Not equal. Note: In some versions of SELECT * FROM Products


<> SQL this operator may be written as !
= WHERE Price <> 18;

SELECT * FROM Products


BETWEEN Between a certain range
WHERE Price BETWEEN 50 AND 60;

SELECT * FROM Customers


LIKE/NOT
Search for a pattern
LIKE
WHERE City LIKE 's%';

SELECT * FROM Customers


To specify multiple possible values for
IN/NOT IN
a column
WHERE City IN ('Paris','London');

 Calculated fields and use of AS keyword

[CC2141L]: [Database Systems]

Page 5
University of Management and Technology

Query:

SELECT staffNo, fName, IName, salary/12


FROM Staff;

AS keyword
The ISO standard allows the column to be named using an AS clause.

Query:

SELECT staffNo, fName, IName, salary/12 AS Msalary


FROM Staff;

 SQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:

 The AND operator displays a record if all the conditions separated by AND are TRUE.


 The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

AND Example

The following SQL statement selects all fields from "Customers" where country is "Germany" AND
city is "Berlin":

Example
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';

[CC2141L]: [Database Systems]

Page 6
University of Management and Technology

OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

OR Example

The following SQL statement selects all fields from "Customers" where city is "Berlin" OR
"München":

Example

SELECT * FROM Customers
WHERE City='Berlin' OR City='München';

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

NOT Example

The following SQL statement selects all fields from "Customers" where country is NOT "Germany":

Example

SELECT * FROM Customers
WHERE NOT Country='Germany';

Lab Task
 Practice SQL AND, OR and NOT Operators.

[CC2141L]: [Database Systems]

Page 7

You might also like