DB Lab4
DB Lab4
Laboratory Manual
[Semester Spring-2023]
Page 1
University of Management and Technology
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 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;
Page 2
University of Management and Technology
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:
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;
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;
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;
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;
Page 4
University of Management and Technology
Page 5
University of Management and Technology
Query:
AS keyword
The ISO standard allows the column to be named using an AS clause.
Query:
The AND and OR operators are used to filter records based on more than one condition:
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';
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.
Page 7