DBMS Lab File
DBMS Lab File
College Code-802
2208020109002
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
INDEX
S.No Practical’s Name Date Remark
1 Write the queries for Data Manipulation and
Data Definition Language. 12/09/2023
2 Write SQL queries using logical operations and
operators. 19/09/2023
2208020109002
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
a. INSERT
b. UPDATE
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
c. DELETE
d. SELECT
DML COMMANDS:
SYNTAX:
INSERT Statement:
Single Row into a Table: INSERT INTO table – name [column- identifier-comma-list)]
VALUES (column-valuecomma-list);
Multiple Row into a Table: insert into <table name> values (&col1, &col2, ….); UPDATE
Statement: UPDATE table-name SET update- column-list [WHERE search- condition];
DELETE Statement: DELETE FROM table-name [WHERE search- condition];
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
b. DROP
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
c. TRUNCATE
d. RENAME
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
e. ALTER
DDL COMMANDS:
SYNTAX:
CREATE Statement: Create table tablename (column_name1 data_ type constraints,
column_name2 data_ type constraints);
DROP:DROP TABLE table_name;
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Add column to Table: ALTER TABLE table_name ADD column_name column-definition;
Modify column in Table: ALTER TABLE table_name MODIFY column_namecolumn_type;
Drop column in Table:ALTER TABLE table_name DROP COLUMN column_name;
DDL QUERIES:
Q1. Write a query to create a table employee with empno, ename, designation, andsalary.
Q2. Write a query for create a from an existing table with all the fields.
Q3. Write a Query to Alter the column EMPNO NUMBER(4) TO EMPNO NUMBER(6).
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
DML QUERIES:
Q1. Write a query to insert the records in to employee.
Q3. Write a query to insert the records in to employee using substitution method.
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Theory:
An operator is a reserved word or a character used primarily in an SQL statement's
WHERE clause to perform operation(s), such as comparisons and arithmetic operations. These
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.
• Arithmeticoperators
• Comparisonoperators
• Logicaloperators
• Operators used to negateconditions
Pre-Requisite Data:
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
CUSTOMER TABLE
Q2. Write a query to find the salary of a person where age is <= 26 or salary > =33000 from
customer table.
SQL>SELECT * FROM CUSTOMERS WHERE AGE <= 26 or SALARY > =33000;
Output:
Q3.Write a query to find the name of customer whose salary is not 30000.
SQL>SELECT * FROM CUSTOMERS WHERE NOT SALARY=30000;
Output:
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Theory:
An SQL group function or aggregate functions performs an operation on a group of
rows and returns a single result. You may want retrieve group of item-prices and return total-
price. This type of scenario is where you would use a group functions. The following table is
summary of some SQL group function & query examples.
To use a group function in a SQL query, list the function name followed by numeric column
name within parentheses. AVG averages the column, COUNT counts the number of items,
MAX returns maximum number of the column, and MIN returns minimum number of the
column .The following is query to retrieve total price, average price, maximum price, and
minimum price from the table “product” assuming the product table has the following values.
QUERY:
PRODUCT TABLE
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Theory:
The ORDER BY clause in SQL will help us to sort the records based on the specific column of a table.
ORDER BY Syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC|RAND();
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
QUERY:
Query to show the list of NAME AND SALARY according to salary in ascending order.
Output:
Query to show the list of NAME AND SALARY according to salary in ascending order.
Output:
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Query to show the list of NAME AND SALARY according to salary in random order.
Output:
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
5. AIM: Write SQL query using order by, GROUP BYclause.
Theory: The MYSQL GROUP BY Clause is used to collect data from multiple records and group the
result by one or more column. It is generally used in a SELECT statement.
You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. on the
grouped column.
CUSTOMER TABLE
Queries:
MySQL GROUP BY Clause with COUNT function:
SELECT ADDRESS, COUNT(ADDRESS) AS “ADDRESS”
FROMCUSTOMERS
GROUP BY ADDRESS;
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
6. AIM: Write SQL queries for sub queries, nestedqueries.
Theory:
Nested Queries: Nesting of queries one within another is known as a nestedqueries.
Sub queries. The query within another is known as a sub query. A statementcontaining sub
query is called parent statement. The rows returned by sub query areused by the parent
statement. Types
1. Sub queries that return several valuesSub queries can also return more than one value. Such
results should be made usealong with the operators in andany.
2. Multiplequeries
Here more than one sub query is used. These multiple sub queries are combined bymeans of
„and‟ & „or‟ keywords
3. Correlated subquery
A sub query is evaluated once for the entire parent statement whereas a correlatedSub query is
evaluated once per row processed by the parentstatement.
Relating Data through Join Concept
The purpose of a join concept is to combine data spread across tables. A join isactually
performed by the „where‟ clause which combines specified rows of tables.Syntax;
selectcolumns from table1, table2 where logical expression; Types of Joins 1.Simple Join 2.Self
Join 3. Outer Join 4. Inner Join
1. Simple Join
a) Equi-join: A join, which is based on equalities, is calledequi-join.
b) Non Equi-join: It specifies the relationship
betweenTableAliases
Table aliases are used to make multiple table queries shorted and more readable. Wegive an
alias name to the table in the „from‟ clause and use it instead of the namethroughout the query.
Self join: Joining of a table to itself is known as self-join. It joins one row in a tableto another.
It can compare each row of the table to itself and also with other rows ofthe same table.
Outer Join: It extends the result of a simple join. An outer join returns all the rowsreturned by
simple join as well as those rows from one table that do not match anyrow from the table. The
symbol (+) represents outerjoins.
Inner join: Inner join returns the matching rows from the tables that are being joined
Queries:
EMPLOYEE TABLE
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Q1. Display all employee names and salary whose salary is greater than minimum salary of the
company and job title starts with „A‟.
SQL>select ename,sal from emp where sal>(select min(sal) from emp where job like 'A%');
Output:
Theory:
A SQL join clause combines column
s from one or more tables in a relational database. It creates a set that can be saved as
a table or used as it is. A JOIN is a means for combining columns from one (self-table) or more
tables by using values common to each. ANSI-standard SQL specifies five types of JOIN:
INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER and CROSS. As a special case, a
table (base table, view, or joined table) can JOIN to itself in aself-join.
A programmer declares a JOIN statement to identify rows for joining. If the evaluated predicate
is true, the combined row is then produced in the expected format, a row set or a temporary
table.
QUERIES:
EMPLOYEE TABLE
DEPARTMENT TABLE
Q1. Display the employee details, departments that the departments are same in both the emp
and dept.
8. AIM: Write a query for extracting data from more than one table.
Query:
EMPLOYEE TABLE
DEPARTMENTTABLE
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Q1.Write a query to extract empno, ename, salary, dname and location from employee and
department table where empno = deptno without using joins.
Q2. Write a query to extract ename, salary and location from employee and department table where
is like 30, 40.
SQL>selectemployee.ename, employee.salary, department.locationFrom
department, employee
Where department.deptnoIN (30,40);
Output:
No rows Selected.
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
9. AIM:Write a query to understand the concepts for ROLL BACK , COMMIT and SAVEPOINT.
Theory:
Transaction Control Language(TCL) commands are used to manage transactions in
database.These are used to manage the changes made by DML statements. It also allows
statements to be grouped together into logical transactions.
Commit command
Commit command is used to permanently save any transaaction into database.
Following is Commit command's syntax,
COMMIT;
Rollback command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction. Following is Rollback command's
syntax, rollback to savepoint-name;
Savepoint command
Savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenevernecessary.
Following is savepoint command's syntax, savepointsavepoint-name;
QUERY:
Q1. Write a query to implement the save point.
SQL> select employee.empno, employee.ename, employee.salary, department.dname,
department.location From department, employee
Where department.deptno = employee.empno;
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Q2. Write a query to implement the Rollback.
SQL> ROLL BACKS1;
Rollbackcomplete.
2108021530011
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
2108021530011