Dbms Practical 1-5
Dbms Practical 1-5
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.
Example-1
Example -2
Roll_no int(4),
First_Name Varchar(20),
Last_Name varchar(20),
Age int(4),
Marks int(4));
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
6) DROP: - It is used to delete an entire object or part of an object from the database.
Example 6-
Example -7
(1,"Prateek","Singhal",19,95),
(2,"Harsh","Bajpai",19,91),
(3,"Rahul","Kumar",19,91),
(4,"Dravid","Son",19,92);
Example-8
Example -9
update student
set marks=93
where First_Name="Harsh";
Example -10
where Roll_no=4;
Practical– 2
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.
Operator Meaning
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
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
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
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
5) LIKE Operator:
In SQL, the LIKE operator is used in the WHERE clause to search for a specified pattern in a column.
Example -5
6) BETWEEN Operator:
The SQL BETWEEN condition allows you to easily test if an expression is within a range of values (inclusive).
Example -6
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
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
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.
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
Example -10
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.
• Arithmetic operator
• Comparison operator
• Logical operator
Operator Description
/ This operator works with the ‘ALL’ keyword and it calculates division operations.
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
2) Substraction Operator:
The SQL Subtraction Operator performs the subtraction on the numerical columns in the table.
Example-2
3) Multiplication Operator:
The SQL Multiplication Operator performs the multiplication on the numerical columns in the table.
Example-3
4) Divide Operator:
The SQL Division operator divides the numerical values of one column by the numerical values of another column.
Example-4
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
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:
OUTPUT-8 OUTPUT-9
OUTPUT-10 OUTPUT-11
• SQL Greater Than Operator (>)
• SQL Greater Than Equals to Operator (>=)
• SQL Less Than Equals to Operator (<=)
Example--6
Example--7
Example--8
Example--9
Example--10
Example--11
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: -
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
1) UNION:
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
UNION
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
UNION ALL
3) INTERSECT: -
Example--3
intersect
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
intersect
Example--5
OUTPUT-8
OUTPUT-9
Relational Database Specific Operation
Relational Algebra:σ(condition)(Relation)
Example--6
Example--7
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.
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
4) Inner Join:
The INNER JOIN keyword selects records that have matching values in both tables.
Example--11
FROM cse
On cse.roll_no=cec.roll_no;
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
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
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
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
AVG Returns the average of all the values in the specified column
MAX Returns the maximum value that occurs in the specified able
MIN Returns the minimum value that occurs in the specified table
1) SUM:-
2) AVG:
3) COUNT:
4) MAX:
5) MIN:
Example 19 - SELECT MIN(age) AS Minimum_Age FROM cse;;
Show databases;
OUTPUT-1,2
OUTPUT-3
Practical-5
Example -1
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
3) Selecting Data with String Concatenation - This query concatenates the FirstName and LastName to create a
full name for each employee.
Example -3
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
Example -5
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
7)Using TRIM() to Remove Extra Spaces – This query removes any leading or trailing spaces from the Email
column.
Example -7
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
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
FROM Employee;
10)Ordering Data Alphabetically - This query sorts the employees alphabetically by LastName in ascending order.
Example -10
FROM Employee