GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
TENTATIVE LIST OF EXPERIMENTS TO BE ATTEMPTED BY THE STUDENTS
DURING THE SESSION
WEEK 1
Program 1 : Study of System Development Life Cycle in developing a database.
Program 2: To study about null statement.
Program 3: Writing Sql statements Using the BETWEEN Keyword.
WEEK 2
Program 4: Writing sql statements using the IN keyword.
Program 5: Study of Like Statement
WEEK 3
Program 6: Joining tables using Equijoin
Program 7: Joining tables using Non Equijoin
WEEK 4
Program 8: To study all sql aggregate functions.
1. AVG
2. SUM
3. MIN
4. MAX
5. COUNT
WEEK 5
Program 9: Using order by clause (sorting data)
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Program 1 : Study of System Development Life Cycle in developing a
database.
Theory:
From concept to production , you can develop a database by using the system development
life cycle which contains multiple stages of development. This top-down, systematic approach
to database development transforms business information requirements into an operational
data base.
1. STRATEGY AND ANALYSIS
Study and analyze the business requirements, interview users and managers to identify
the information and requirements. Incorporate the enterprise and application mission
statements as well as future system specifications.
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Built models of the system. Transfer the business narrative into a graphical
representation of business information needs and rules. Confirm and refine the model
with the analysts and experts .
2. DESIGN
Design the database based on the model developed in the strategy and analysis
phase.
3. BUILT AND DOCUMENT
Built the prototype system. Write and execute the commands to create the
tables and supporting Objects for the database.
Develop user documentation; help text and operations manuals to support the
use and operation of the system.
4. TRANSITION
Refine the prototype. Move an application into production with user acceptance testing,
conversion of existing data and parallel operations. Make any modifications required.
5. PRODUCTION
Roll out the system to the users .operate the production system. Monitor its performance,
and enhance and refine the system.
Program 2: To study about null statement.
Theory:
If a row lacks data value for a particular column , that value is said to be null , or to contain a
null . Zero is a number, and space is a character. Columns of any data type can contain nulls.
However, some constraints, NOT NULL and PRIMARY KEY, prevent nulls from being used in the
column. For example, if you attempt to perform division with zero, you get an error. However, if
you divide a number by null, the result is a null or unknown.
Query 1:
select department_id, employee_id, last name
From employees
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Where department_id is null;
OUTPUT
DEPARTMENT_ID EMPLOYEE_ID LAST_NAME
178 Grant
Query 2:
Select last_name, department_id +20, department_id *20
From employees
Where department_id is null;
OUTPUT
DEPARTMENT_ID+20 DEPARTMENT_ID*20
Program 3: Writing Sql statements Using the BETWEEN Keyword.
Theory:
Values specified with the BETWEEN condition are inclusive. You must specify the lower limit
first.
Syntax:
Select col1,col2..
From table_name
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Where col_name between lower_limit AND upper_limit.
Example:
SELECT last_name salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
Program 4: Writing sql statements using the IN keyword.
Theory:
The IN condition also known as the membership condition.
The IN condition can be used with any data types.
If characters are dates’ are used in the list, they must be enclosed in single quotation marks (‘’).
Query:
SELECT employee_id, last_name, salary,manager_id
FROM employees
WHERE manager_id IN (100,101,201);
Program 5: Study of Like Statement
Theory:
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
The LIKE operator is used to search for a specified pattern in a column.
SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
WHERE column_name LIKE pattern
LIKE Operator Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Now we want to select the persons living in a city that starts with "s" from the table above.
Query:
SELECT * FROM Persons
WHERE City LIKE 's%'
The "%" sign can be used to define wildcards (missing letters in the pattern) both before and
after the pattern.
OUTPUT
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
3 Pettersen Kari Storgt 20 Stavanger
Next, we want to select the persons living in a city that ends with an "s" from the "Persons"
table.
Query:
SELECT * FROM Persons
WHERE City LIKE '%s'
OUTPUT
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
Next, we want to select the persons living in a city that contains the pattern "tav" from the
"Persons" table.
Query:
SELECT * FROM Persons
WHERE City LIKE '%tav%'
OUTPUT
P_Id LastName FirstName Address City
3 Pettersen Kari Storgt 20 Stavanger
It is also possible to select the persons living in a city that NOT contains the pattern "tav" from
the "Persons" table, by using the NOT keyword.
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Query:
SELECT * FROM Persons
WHERE City NOT LIKE '%tav%'
Output
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
SQL Wildcards
SQL wildcards can be used when searching for data in a database.
SQL wildcards can substitute for one or more characters when searching for data in a
database.
SQL wildcards must be used with the SQL LIKE operator.
With SQL, the following wildcards can be used:
Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
SQL Wildcard Examples
We have the following "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
3 Pettersen Kari Storgt 20 Stavanger
Using the % Wildcard
Now we want to select the persons living in a city that starts with "sa" from the "Persons" table.
Query:
SELECT * FROM Persons
WHERE City LIKE 'sa%'
OUTPUT
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
Next, we want to select the persons living in a city that contains the pattern "nes" from the
"Persons" table.
Query:
SELECT * FROM Persons
WHERE City LIKE '%nes%'
OUTPUT
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
2 Svendson Tove Borgvn 23 Sandnes
Using the _ Wildcard
Now we want to select the persons with a first name that starts with any character, followed by
"la" from the "Persons" table.
Query:
SELECT * FROM Persons
WHERE FirstName LIKE '_la'
OUTPUT
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
Next, we want to select the persons with a last name that starts with "S", followed by any
character, followed by "end", followed by any character, followed by "on" from the "Persons"
table.
Query:
SELECT * FROM Persons
WHERE LastName LIKE 'S_end_on'
OUTPUT
P_Id LastName FirstName Address City
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
2 Svendson Tove Borgvn 23 Sandnes
Program 6 : Joining tables using Equijoin
Theory:
Rows of one table can be joined to rows of another table using Equijoin, when common
values exist in corresponding columns of those tables ,that is usually primary and foreign
key columns.
Joining condition use = operator.
To join n tables, we need at least n-1 join conditions.
Equijoins are also called simple or inner joins.
Syntax:
Select table1.column,table2.column…
From table1,table2
Where table1.column1=table2.column2;
Tables used :
The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
The "Orders" table:
O_Id OrderNo P_Id
1 77895 3
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Query:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
where Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName;
OUTPUT
LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
Program 7: Joining tables using Non Equijoin
Theory:
Joining condition use any operator other than ‘=’.
To join n tables, we need at least n-1 join conditions.
Query:
Select e.last_name, e. salary, j.grades
From employees e,job_grades j
Where e.salary between j.lowest_sal and j.highest_sal;
Program 8: To study all sql aggregate functions.
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Theory:
SQL aggregate functions return a single value, calculated from values in a column.
Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
Syntax:
SELECT AVG(column_name) FROM table_name
Query
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders);
Now we want to find the customers that have an OrderPrice value higher than the average
OrderPrice value.We use the following SQL statement:
OUTPUT
Customer
Hansen
Nilsen
Jensen
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Object: Using COUNT aggregate function in sql statements.
Theory:
The COUNT() function returns the number of rows that matches a specified criteria.
Syntax
SELECT COUNT(column_name) FROM table_name
The COUNT(column_name) function returns the number of values (NULL values will not
be counted) of the specified column.
SELECT COUNT(*) FROM table_name
The COUNT(*) function returns the number of records in a table;
SELECT COUNT(DISTINCT column_name) FROM table_name
The COUNT(DISTINCT column_name) function returns the number of distinct values of
specified column.
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft
Access.
We have the following "Orders" table:
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Now we want to count the number of orders from "Customer Nilsen".We use the following SQL
statement:
Object: Using MAX aggregate function in sql statements.
Theory:
The MAX() function returns the largest value of the selected column.
Syntax
SELECT MAX(column_name) FROM table_name;
We have the following "Orders" table:
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Now we want to find the largest value of the "OrderPrice" column.We use the following SQL
statement:
Query:
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
OUTPUT
LargestOrderPrice
2000
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Object: Using MIN aggregate function in sql statements.
Theory:
The MIN() function returns the smallest value of the selected column.
Syntax:
SELECT MIN(column_name) FROM table_name
We have the following "Orders" table:
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Now we want to find the smallest value of the "OrderPrice" column.We use the following SQL
statement:
Query:
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders;
OUTPUT
SmallestOrderPrice
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
100
Object: Using SUM aggregate function in sql statements.
Theory:
The SUM() function returns the total sum of a numeric column.
Syntax:
SELECT SUM(column_name) FROM table_name
We have the following "Orders" table:
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Now we want to find the sum of all "OrderPrice" fields".We use the following SQL statement:
Query:
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
OUTPUT
OrderTotal
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
5700
Query 1:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Nilsen’
The result of the SQL statement above will be 2, because the customer Nilsen has made 2
orders in total.
OUTPUT
CustomerNilsen
Query 2:
SELECT COUNT(*) AS NumberOfOrders FROM Orders;
OUTPUT
NumberOfOrders
which is the total number of rows in the table.
Query 3:
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders;
Now we want to count the number of unique customers in the "Orders" table.We use the
following SQL statement:
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
OUTPUT
NumberOfCustomers
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.
Program 9: Using order by clause (sorting data)
Theory:
The ORDER BY keyword is used to sort the result-set.
The ORDER BY keyword is used to sort the result-set by a specified column.
The ORDER BY keyword sort the records in ascending order by default.
If you want to sort the records in a descending order, you can use the DESC keyword.
Syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
ORDER BY Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Tom Vingvn 23 Stavanger
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
Now we want to select all the persons from the table above, however, we want to sort the
persons by their last name.
Query:
SELECT * FROM Persons
ORDER BY LastName
OUTPUT
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
4 Nilsen Tom Vingvn 23 Stavanger
3 Pettersen Kari Storgt 20 Stavanger
2 Svendson Tove Borgvn 23 Sandnes
ORDER BY DESC Example
Now we want to select all the persons from the table above, however, we want to sort the
persons descending by their last name.
Query:
SELECT * FROM Persons
ORDER BY LastName DESC
OUTPUT
P_Id LastName FirstName Address City
59
GOVERNMENT ENGINEERING COLLEGE, BHARATPUR DBMS LAB IVTH SEM CSE
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Tom Vingvn 23 Stavanger
1 Hansen Ola Timoteivn 10 Sandnes
59