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

Dbms Practical 1-5

Uploaded by

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

Dbms Practical 1-5

Uploaded by

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

UTTARANCHAL UNIVERSITY

(Established vide Uttaranchal University Act, 2012)


(Uttarakhand Act No. 11 of 2013)
Arcadia Grant, P.O. Chandanwari, Premnagar, Dehradun, Uttarakhand

INDEX
College/University: UIT/Uttaranchal University Semester/Year: 3th Sem/2rd Year

Teacher,s
S.No. Statement Page No.
Date sign

10

11

12

13

14

15
OUTPUT-1 OUTPUT-2

OUTPUT-3 OUTPUT-4
Practical– 1

Aim- Write the queries for Data Definition and Data Manipulation
Language.

Data Definition Language


DDL (Data Definition Language) is a type of SQL command used to define data structures and modify data. It
creates, alters, and deletes database objects such as tables, views, indexes, and users. Examples of DDL statements
include CREATE, ALTER, DROP and TRUNCATE.

Types of DDL Statements


1) CREATE: - It is used to create objects in the database, such as tables, views, stored procedures, and more.

Example-1

Create Database College;

Example -2

Create table Student(

Roll_no int(4),

First_Name Varchar(20),

Last_Name varchar(20),

Age int(4),

Marks int(4));

2) ALTER: - It is used to modify the structure of an existing database object.


Example-3

ALTER table student

add Phone_no bigint(11);

3) RENAME: - Used to rename an existing database object.


Example -4

Rename table Student to Student_Details;


OUTPUT-5 OUTPUT-6

OUTPUT-7,8 OUTPUT-9

OUTPUT-10
4) TRUNCATE: - Used to delete all records from a table but does not delete the table structure.

Example -5

Truncate table Student_Details;

6) DROP: - It is used to delete an entire object or part of an object from the database.

Example 6-

Drop table Student_details;

Data Manipulation Language


DML (Data Manipulation Language) is a type of SQL command used to manipulate data in a database. It inserts,
updates, and deletes data from a database table. Types of DML statements include INSERT, UPDATE, and DELETE.

Types of DML Statements:

1) INSERT:- Used to add new records to a database table.

Example -7

Insert into student values

(1,"Prateek","Singhal",19,95),

(2,"Harsh","Bajpai",19,91),

(3,"Rahul","Kumar",19,91),

(4,"Dravid","Son",19,92);

2) SELECT:- Used to retrieve data from one or more tables in a database.

Example-8

Select * from student;

3) UPDATE:- Used to modify existing records in a database table.

Example -9

update student

set marks=93

where First_Name="Harsh";

4) DELETE: - Used to delete existing records from a database table.

Example -10

Delete from Student

where Roll_no=4;
Practical– 2

Aim- Write SQL queries using logical operators

Logical Operator:
SQL logical operators are used to test for the truth of the condition. A logical operator like the Comparison operator
returns a Boolean value of TRUE, FALSE, or UNKNOWN. In this practical, we will discuss different types of Logical
Operators.

Logical operators are used to combine or manipulate the conditions given in a query to retrieve or manipulate data
.there are some logical operators in SQL like OR, AND etc.

Types of Logical Operators in SQL


Given below is the list of logical operators available in SQL

Operator Meaning

AND TRUE if both Boolean expressions are TRUE.

IN TRUE if the operand is equal to one of a list of expressions.

NOT Reverses the value of any other Boolean operator.

OR TRUE if either Boolean expression is TRUE.

LIKE TRUE if the operand matches a pattern.

BETWEEN TRUE if the operand is within a range.

ALL TRUE if all of a set of comparisons are TRUE.

ANY TRUE if any one of a set of comparisons is TRUE.

EXISTS TRUE if a subquery contains any rows.

SOME TRUE if some of a set of comparisons are TRUE.


OUTPUT-1 OUTPUT-2

OUTPUT-3 OUTPUT-4

OUTPUT-5

OUTPUT-6
1) AND Operator:
The AND operator is used to combines two or more conditions but if it is true when all the conditions are satisfied.

Example-1

select * from student where Age=19 AND Marks>90;

2) IN Operator:
It is used to remove the multiple OR conditions in SELECT, INSERT, UPDATE, or DELETE. and We can also use
NOT IN to minimize the rows in your list and any kind of duplicate entry will be retained.

Example -2

select * from student where Marks IN(91,92,93);

3) NOT Operator:
The NOT operator is used in combination with other operators to give the opposite result, also called the negative
result.

Example -3

select * from student where NOT Age=19;

4) OR Operator:
The OR operator is used to combines two or more conditions but if it is true when one of the conditions are satisfied.

Example -4

select * from student where Age=19 OR Marks<90;

5) LIKE Operator:
In SQL, the LIKE operator is used in the WHERE clause to search for a specified pattern in a column.

% – It is used for zero or more than one character.

Example -5

select * from student where First_Name LIKE "R%";

6) BETWEEN Operator:
The SQL BETWEEN condition allows you to easily test if an expression is within a range of values (inclusive).

Example -6

select * from student where Marks BETWEEN 86 AND 93;


OUTPUT-7

OUTPUT-8

OUTPUT-9

OUTPUT-10
7) ALL Operator:
The ALL operator returns TRUE if all of the subqueries values matches the condition. All operator is used with
SELECT, WHERE, HAVING statement.

Example -7

select First_Name,Last_Name,Age

from student

where Age>=ALL(Select Age from student where Marks=93)

ORDER BY Age DESC;

8) ANY Operator:
It returns a boolean value as a result. It returns TRUE if ANY of the subquery values match the condition.

Example -8

select First_Name,Last_Name,Age

from student

where Age>=ANY(Select Age from student where Marks<90 GROUP BY Roll_no)

ORDER BY First_Name,Last_Name;

9) EXISTS Operator:
In SQL,Exists operator is used to check whether the result of a correlated nested query is empty or not.

Exists operator is used with SELECT, UPDATE, INSERT or DELETE statement.

The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns
TRUE if the subquery returns one or more records.

Example -9

select First_Name,Last_Name

from student

where EXISTS(Select First_Name,Last_Name from Student where Age=19);

10) SOME Operator:


In SQL, SOME operators are issued with comparison operators (<,>,=,<=, etc) to compare the value with the result of
a subquery.

Example -10

select * from Student

where Age< SOME(Select Age from student where First_Name="Prateek");


Practical– 3

Aim- Write SQL queries using SQL operators.

SQL Operator
SQL Operators perform arithmetic, comparison, and logical operations to manipulate and retrieve data from
databases.

Operators in SQL:

Operators in SQL are symbols that help us to perform specific mathematical and logical computations on operands. An
operator can either be unary or binary.

The unary operator operates on one operand, and the binary operator operates on two operands.

Types of Operators in SQL

Different types of operators in SQL are:

• Arithmetic operator

• Comparison operator

• Logical operator

SQL Arithmetic Operators:


Arithmetic operators in SQL are used to perform mathematical operations on numeric values in queries. Some
common arithmetic operators are:

Operator Description

+ The addition is used to perform an addition operation on the data values.

– This operator is used for the subtraction of the data values.

/ This operator works with the ‘ALL’ keyword and it calculates division operations.

* This operator is used for multiplying data values.

% Modulus is used to get the remainder when data is divided by another.


OUTPUT-1 OUTPUT-2

OUTPUT-3 OUTPUT-4

OUTPUT-5
1) Addition Operators:
The SQL Addition Operator performs the addition on the numerical columns in the table.

Example-1

Select fees+Extra_fees as Total_fees from student;

2) Substraction Operator:
The SQL Subtraction Operator performs the subtraction on the numerical columns in the table.

Example-2

Select fees-Extra_fees as Discounted_fees from student;

3) Multiplication Operator:
The SQL Multiplication Operator performs the multiplication on the numerical columns in the table.

Example-3

Select num1*num2 as Table_of_2 from Tables;

4) Divide Operator:
The SQL Division operator divides the numerical values of one column by the numerical values of another column.

Example-4

Select Table_of_2/num1 as Table_of_2_divide_num1 from Tables;

5) Modulo Operator:
The SQL Modulus Operator provides the remainder when the numerical values of one column are divided by the
numerical values of another column.

Example-5

Select Table_of_2%num2 as Table_of_2_Modulo_num2 from Tables;

SQL Comparison Operators:


The SQL Operators which compare the values of two columns in the database tables are called as comparison
operators.

In SQL, comparison operators are always used in the WHERE clause with the SELECT, UPDATE, and DELETE
statements.

The comparison operators in SQL are categorized into the following six operators category:

• SQL Equal Operator (=)


• SQL Not Equal Operator (!=)
• SQL Less Than Operator (<)
OUTPUT-6 OUTPUT-7

OUTPUT-8 OUTPUT-9

OUTPUT-10 OUTPUT-11
• SQL Greater Than Operator (>)
• SQL Greater Than Equals to Operator (>=)
• SQL Less Than Equals to Operator (<=)

1) SQL Equal Operator (=):


This operator selects only those data from the table which matches the specified value.

Example--6

select * from Student Where Age=19;

2) SQL NOT Equal Operator (!=):


This operator selects only those data from the table which does not match with the specified value.

Example--7

select * from Student Where Age!=19;

3) SQL Greater Than Operator (>):


This operator selects, modifies, and deletes only those data from the table which are greater than the value specified in
the query.

Example--8

select * from Student Where Marks>90;

4) SQL Less Than Operator (<):


This operator in SQL selects only those data from the table which are less than the given value.

Example--9

select * from Student Where Marks<90;

5) SQL Greater Than Equals to Operator (>=):


This operator is used to select records where a column value is greater than or equal to a specified value.

Example--10

select * from Student Where Marks>=93;

6) SQL Less Than Equals to Operator (<=):


This operator selects only those data from the table which are less than and equal to the given value.

Example--11

select * from Student Where Marks<=93;


Practical-4
Aim- Write SQL queries for relational Algebra.

Relational Algebra
RELATIONAL ALGEBRA is a widely used procedural query language. It collects instances of relations as input and
gives occurrences of relations as output. It uses various operations to perform this action. SQL Relational algebra query
operations are performed recursively on a relation. The output of these operations is a new relation, which might be
formed from one or more input relations.

Set Operator
The Set Operator allows to perform algebraic operation among two or more tables or relation and produce a result
depending on its operators. It contains four different operators: -

Operators covered under SET operators are:

1. UNION

2. UNION ALL

3. INTERSECT

4. MINUS

1) UNION:

o UNION will be used to combine the result of two select statements.

o Duplicate rows will be eliminated from the results obtained after performing the UNION operation.
OUTPUT-1 OUTPUT-2

OUTPUT-3 OUTPUT-4

OUTPUT-5,7
Example--1

select * from cseeq

UNION

select * from cec;

2) UNION ALL:

o This operator combines all the records from both the queries.

o Duplicate rows will be not be eliminated from the results obtained after performing the UNION ALL operation.

Example--2

select * from cse

UNION ALL

select * from cec;

3) INTERSECT: -

o It is used to combine two SELECT statements, but it only returns the


records which are common from both SELECT statements.

Example--3

select * from tab1

intersect

select * from tab2;

4) MINUS:
o It displays the rows which are present in the first query but absent in the second query with no duplicates.

Example--4

select * from tab1

intersect

select * from tab2;

5) Cartesian Product (`×`) :

Example--5

SELECT * FROM cse,cec where age>19;


OUTPUT-6 OUTPUT-7

OUTPUT-8

OUTPUT-9
Relational Database Specific Operation

1) Selection (`σ`) - Filtering rows

Relational Algebra:σ(condition)(Relation)

Example--6

Relational Algebra: σ(age > 19)(CSE)

SELECT * FROM Employee WHERE age > 19;

2) Projection (`π`) - Selecting columns

Relational Algebra: π(column1, column2, ...)(Relation)

Example--7

Relational Algebra: π(name, age)(Employee)

SELECT * FROM cse,cec where age>19;

JOINS

1) Equi JOIN:

An Equi Join in SQL is a type of join that combines rows from two or more tables based on a common column or set
of columns, using the equality operator = to compare column values.

Example--8

Select cse.first_name,cse.age,cec.hobby

FROM cse,cec

Where ces.roll_no=cec.roll_no;

2) Non-Equi Join-

The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <, >=, <= along with
conditions.

set of columns, using the equality operator = to compare column values.

Example--9

Select cse.first_name,cse.age,cec.hobby

FROM cse,cec

Where ces.roll_no>cec.roll_no;
OUTPUT-10 OUTPUT-11

OUTPUT-12 OUTPUT-13
3) Natural Join:
Same as Equi Join but writing method is different (results will be same as equi-join).

Example--10

Select cse.first_name,cse.age,cec.hobby

FROM cse

Natural Join cec;

4) Inner Join:

The INNER JOIN keyword selects records that have matching values in both tables.

Example--11

Select cse.roll_no,cse.first_name, cse.last_name ,cse.age,cec.hobby

FROM cse

Inner Join cec

On cse.roll_no=cec.roll_no;

5) SQL Cross join:

The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number
of rows in the second table if no WHERE clause is used along with CROSS JOIN.

Example--12

Select cse.first_name,cse.age,cec.hobby

FROM cse

Cross Join cec;

6) Left-Outer Join-

Left Outer Join returns all the rows from the table on the left and the columns of the table on the right are null padded.
Left Outer Join retrieves all the rows from both the tables that satisfy the join condition along with the unmatched
rows of the left table.

Example--13

Select cse.first_name,cse.age,cec.hobby

FROM cse

Left Outer Join cec

On cse.roll_no=cec.roll_no;
OUTPUT-14

OUTPUT-15 OUTPUT-16

OUTPUT-17,18 OUTPUT-19
7) Right-Outer Join-

A RIGHT OUTER JOIN in MySQL returns all rows from the right table, along with matching rows from the left
table. If no match is found, `NULL` values are returned for columns from the left table.

Example--14

Select cse.first_name,cse.age,cec.hobby

FROM cse

Right Outer Join cec

On cse.roll_no=cec.roll_no;

Set Function
SQL functions are built-in operations that you can use to perform calculations, manipulate data, or query information.

Function Effect

SUM Adds up the values in a specified column

AVG Returns the average of all the values in the specified column

COUNT Returns the number of rows in the specified table

MAX Returns the maximum value that occurs in the specified able

MIN Returns the minimum value that occurs in the specified table

1) SUM:-

Example 15 - SELECT SUM(Car_Price) AS Total_Price FROM cars;

2) AVG:

Example 16 - SELECT AVG(age) AS Average_Age FROM cse;

3) COUNT:

Example 17 - SELECT COUNT(*) AS Total_Students FROM cse;

4) MAX:

Example 18 - SELECT MAX(age) AS Maximum_Age FROM cse;

5) MIN:
Example 19 - SELECT MIN(age) AS Minimum_Age FROM cse;;
Show databases;

OUTPUT-1,2

OUTPUT-3
Practical-5

Aim- SQL query using character.


1) Creating a Table with Character Data Types –

 VARCHAR(50) allows variable-length characters up to 50 characters.


 CHAR(50) fixes the size to 50 characters, padding with spaces if needed.

Example -1

CREATE TABLE Employees (

EMP_ID INT PRIMARY KEY,

First_Name VARCHAR(10),

Last_Name VARCHAR(10),

Email CHAR(20),

Department VARCHAR(10)

);

2) Inserting Data into the Table - This query inserts multiple rows of data into the Employees table with
character data.

Example -2

INSERT INTO Employee (EMP_ID, First_Name, Last_Name, Email, Department)

VALUES (1, 'manish', 'danu', 'manish2gmail.com', 'cse'),

(2, 'shubham ', 'mehra', '[email protected]', 'civil'),

(3, 'mansi', 'singh', '[email protected]', 'mechanical');

3) Selecting Data with String Concatenation - This query concatenates the FirstName and LastName to create a
full name for each employee.

Example -3

SELECT CONCAT(First_Name, , Last_Name) AS FullName, Department FROM Employee;


OUTPUT-4

OUTPUT-5

OUTPUT-6

OUTPUT-7

OUTPUT-8
4)Using the LIKE Operator to Find Substrings - This query selects employees whose email ends with
example.com. The % wildcard is used to match any sequence of characters before example.com.

Example -4

SELECT *

FROM Employee

WHERE Email LIKE '%example.com';

5)Using String Functions –

 UPPER() converts the FirstName to uppercase.

 LOWER() converts the LastName to lowercase.

 SUBSTRING() extracts the first 5 characters from the Email.

Example -5

SELECT UPPER(First_Name), LOWER(Last_Name), SUBSTRING(Email, 1, 5) AS EmailPrefix

FROM Employee;

6)Updating a Column with a Character String – This query updates the Department column for all employees
where the department is listed as 'HR' to 'Human Resources'.

Example -6

UPDATE Employee

SET Department = 'electrical'

WHERE Department = 'civil’';

7)Using TRIM() to Remove Extra Spaces – This query removes any leading or trailing spaces from the Email
column.

Example -7

SELECT TRIM(Email) AS CleanEmail

FROM Employee;

8)Using CHAR_LENGTH() to Get the Length of a String - This query returns the length of the Email for each
employee.

Example -8

SELECT LENGTH(‘Email’) AS EmailLength;


OUTPUT-9

OUTPUT-10
9)Using REPLACE() to Modify Part of a String - This query replaces the domain example.com with company.com
in the Email addresses.

Example -9

SELECT REPLACE(Email, 'gmail.com', 'company.com') AS NewEmail

FROM Employee;

10)Ordering Data Alphabetically - This query sorts the employees alphabetically by LastName in ascending order.

Example -10

SELECT First_Name, Last_Name, Department

FROM Employee

ORDER BY Last_Name ASC;

You might also like