DBMS-UnitIV - Final
DBMS-UnitIV - Final
SQL data types are used to define the type of data that can be stored in a column of a
database table. These data types ensure that data is stored in an efficient and consistent
manner. Below is a brief overview of common SQL data types:
1|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
CHAR(n): Fixed-length string, where n specifies the length (e.g., CHAR(10) stores
exactly 10 characters).
VARCHAR2(n): Variable-length string, where n specifies the maximum length
(e.g., VARCHAR(255) stores up to 255 characters).
TEXT: Stores large strings, typically for unstructured data like paragraphs of text.
BOOLEAN / BOOL: Stores TRUE or FALSE values (in some systems, represented
as 1 for TRUE and 0 for FALSE).
6. UUID
UUID: Stores Universally Unique Identifier (UUID) values, which are 128-bit
numbers used to uniquely identify information in a distributed system.
ENUM: Stores one value from a predefined list of values (e.g., ENUM('small',
'medium', 'large')).
SET: Similar to ENUM, but allows storing multiple values from a predefined list.
XML: Stores XML data, enabling structured document storage.
ARRAY: Stores an array of values, supported in some SQL databases.
SERIAL: Often used for auto-incrementing primary keys, where each new row gets
a unique integer value.
GEOMETRY: Stores geometric data (e.g., points, lines, polygons) for spatial
databases.
2|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
Example:
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
3|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is
also used to add and drop various constraints on the existing table.
ALTER TABLE – ADD
ADD is used to add columns into the existing table.
Syntax: ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,
…
Columnname_n datatype);
DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from
the table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified
at once.
Example:
Alter table students modify remarks char(25);
ALTER TABLE-RENAME
The RENAME clause within the ALTER command specifically allows you to rename a
table or a column within a table.
1. Renaming a Table
The ALTER TABLE statement with the RENAME clause is used to rename an existing table to a
new name.
Syntax: ALTER TABLE old_table_name RENAME TO new_table_name;
Example: ALTER TABLE Employees RENAME TO Staff;
After executing this command, the table Employees will be renamed to Staff.
2. Renaming a Column
The ALTER TABLE statement can also be used to rename a column in a table.
4|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
After executing this command, the column empName will be renamed to employeeName in
the Employees table.
DROP
DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view.
Syntax: DROP TABLE table_name;
table_name: Name of the table to be deleted.
RENAME
The RENAME command in SQL is used to change the name of an existing database object,
such as a table, view, or index. It is a straightforward and useful command that helps in
renaming objects without altering their structure or data.
1.RENAME a Table
The RENAME command is often used to rename a table.
2.RENAME a View
The RENAME command can also be used to rename a view.
5|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
SELECT
It is used to retrieve data from the database.
Syntax: SELECT column1, column2 FROM table_name
Example: SELECT ROLL_NO, NAME, AGE FROM Student;
The INSERT INTO statement of SQL is used to insert a new row in a table. There are two
ways of using INSERT INTO statement for inserting rows:
1.Only values: First method is to specify only the value of data to be inserted without
the column names.
Syntax : INSERT INTO table_name VALUES (value1, value2, value3,…);
Example:
INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST BENGAL’,’XXXXXXXXXX’,’19’);
2.Column names and values both: In the second method we will specify both the columns
which we want to fill and their corresponding values as shown below:
Syntax :
INSERT INTO table_name (column1, column2, column3,..) VALUES (value1, value2,
value3,….);
Example :
INSERT INTO Student (Roll_no, Name, Age) VALUES (‘5′,’PRATIK’,’19’);
UPDATE Statement
The UPDATE statement in SQL is used to update the data of an existing table in database.
We can update single columns as well as multiple columns using UPDATE statement as per
our requirement.
Syntax :
UPDATE table_name SET column1 = value1, column2 = value2,... WHERE
condition;
Example: UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;
DELETE Statement
The DELETE Statement in SQL is used to delete existing records from a table. We can
delete a single record or multiple records depending on the condition we specify in the
WHERE clause.
Syntax: Delete from tablename;
6|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
2. Arithmetic Operations
You can perform arithmetic operations directly within the SELECT statement.
QUERY : SELECT first_name, last_name, salary, salary * 0.1 AS bonus FROM
Employees WHERE age > 30;
This query selects the first_name, last_name, salary, and calculates a bonus as 10% of the
salary for employees older than 30.
4. Aggregation Functions
Aggregation functions such as COUNT, SUM, AVG, MIN, and MAX are used to perform
calculations on a set of values.
QUERY : SELECT department, AVG(salary) AS average_salary FROM Employees
WHERE age > 30
GROUP BY department;
This query calculates the average salary of employees older than 30, grouped by their
department.
7|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
Aggregate functions in Oracle SQL are used to perform calculations on a group of rows and
return a single value.
1. COUNT()
The COUNT() function counts the number of rows or non-NULL values in a column.
Query: Count the total number of employees.
Output:
TOTAL_EMPLOYEES
6
2. SUM()
The SUM() function calculates the total sum of a numeric column.
Query: Calculate the total salary paid to all employees.
SELECT SUM(SALARY) AS TOTAL_SALARY
FROM EMPLOYEES;
Output:
TOTAL_SALARY
28500
3. AVG()
The AVG() function calculates the average value of a numeric column.
8|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
4. MAX()
The MAX() function returns the maximum value from a column.
Query: Find the highest salary among employees.
SELECT MAX(SALARY) AS HIGHEST_SALARY
FROM EMPLOYEES;
Output:
HIGHEST_SALARY
7000
5. MIN()
The MIN() function returns the minimum value from a column.
Query: Find the lowest salary among employees.
SELECT MIN(SALARY) AS LOWEST_SALARY
FROM EMPLOYEES;
Output:
LOWEST_SALARY
4000
SELECT
DEPARTMENT,
COUNT(*) AS EMPLOYEE_COUNT,
SUM(SALARY) AS TOTAL_SALARY,
AVG(SALARY) AS AVERAGE_SALARY,
MAX(SALARY) AS HIGHEST_SALARY,
MIN(SALARY) AS LOWEST_SALARY
FROM EMPLOYEES
GROUP BY DEPARTMENT;
Output:
9|P a ge
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is
also used to add and drop various constraints on the existing table.
ALTER TABLE – ADD
ADD is used to add columns into the existing table.
Syntax: ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,
…
Columnname_n datatype);
DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from
the table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified
at once.
Example:
Alter table students modify remarks char(25);
ALTER TABLE-RENAME
The RENAME clause within the ALTER command specifically allows you to rename a
table or a column within a table.
1. Renaming a Table
The ALTER TABLE statement with the RENAME clause is used to rename an existing table to a
new name.
Syntax: ALTER TABLE old_table_name RENAME TO new_table_name;
Example: ALTER TABLE Employees RENAME TO Staff;
After executing this command, the table Employees will be renamed to Staff.
2. Renaming a Column
The ALTER TABLE statement can also be used to rename a column in a table.
10 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
After executing this command, the column empName will be renamed to employeeName in
the Employees table.
SQL JOINS :
SQL joins are used to combine rows from two or more tables based on a related column
between them. Different types of joins determine how data from these tables is combined.
Example Tables:
Table Employees:
EmployeeID Name DepartmentID
1 Alice 10
2 Bob 20
3 Charlie 30
4 Dave NULL
Table Departments:
DepartmentID DepartmentName
10 HR
20 Finance
30 IT
40 Marketing
1. INNER JOIN
Definition: An INNER JOIN returns only the rows that have
matching values in both tables.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Example :
Output:
Name DepartmentName
Alice HR
Bob Finance
Charlie IT
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Example :
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
Name DepartmentName
Alice HR
Bob Finance
Charlie IT
Dave NULL
Syntax:
SELECT columns
FROM table1
12 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
Example Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
Name DepartmentName
Alice HR
Bob Finance
Charlie IT
NULL Marketing
Definition: A FULL JOIN returns all rows when there is a match in either the left or right
table. If there is no match, the result is NULL from the
non-matching side.
Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
Example Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
Name DepartmentName
Alice HR
Bob Finance
Charlie IT
Dave NULL
NULL Marketing
5. CROSS JOIN
Definition: A CROSS JOIN returns the Cartesian product of the two tables, meaning it
combines all rows from the left table with all rows from the right table.
13 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Output:
Name DepartmentName
Alice HR
Alice Finance
Alice IT
Alice Marketing
Bob HR
Bob Finance
Bob IT
Bob Marketing
Charlie HR
Charlie Finance
Charlie IT
Charlie Marketing
Dave HR
Dave Finance
Dave IT
Dave Marketing
6. SELF JOIN
Definition: A SELF JOIN is a join of a table with itself. This is useful when the table has a
hierarchical relationship.
Syntax:
SELECT A.column, B.column
FROM table A
INNER JOIN table B
ON A.column = B.column;
Example Query:
SELECT A.Name AS Employee, B.Name AS Manager
FROM Employees A
INNER JOIN Employees B
ON A.ManagerID = B.EmployeeID;
Output: This would list employees alongside their managers, assuming the Employees table
has a ManagerID column referencing the EmployeeID.
A SELF JOIN is conceptually similar to an INNER JOIN, but it's between two copies of the
same table. There's no specific Venn diagram since it's the same table joined with itself.
14 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
The SQL Set operation is used to combine the two or more SQL SELECT statements.
Types of Set Operations
1. Union
2. UnionAll
3. Intersect
4. Minus
1. Union
The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
In the union operation, all the number of datatype and columns must be same in both
the tables on which UNION operation is being applied.
The union operation eliminates the duplicate rows from its result set.
ID NAME
ID NAME
3
1 Jack
2 Harry
4 Stephan
3 Jackson
5 David
Union SQL query will be:
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.
Syntax:
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
16 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
3. Intersect
It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
In the Intersect operation, the number of datatype and columns must be the same.
It has no duplicates and it arranges the data in ascending order by default.
Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Intersect query will be:
SELECT * FROM First
INTERSECT
SELECT * FROM Second;
3 Jackson
4. Minus
It combines the result of two SELECT statements. Minus operator is used to display
the rows which are present in the first query but absent in the second query.
It has no duplicates and data arranged in ascending order by default.
Syntax:
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;
Example
Using the above First and Second table.
Minus query will be:
17 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
ID NAME
1 Jack
2 Harry
A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the data were
coming from one single table.
Note: A view always shows up-to-date data! The database engine recreates the view, every
time a user queries it.
The following SQL creates a view that shows all customers from Brazil:
A Subquery is a query within another SQL query and embedded within the WHERE clause.
18 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
Important Rule:
o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the main
query, and the inner query is known as a subquery.
o Subqueries are on the right side of the comparison operator.
o A subquery is enclosed in parentheses.
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command
can be used to perform the same function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
Syntax
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
19 | P a g e
Database Management Systems-IVSem-UNIT IV-Course 9 NSCS/NSST
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
*******
20 | P a g e