Unit 3 DB
Unit 3 DB
Unit-3 :
SQL:- INTRODUCTION:
• Structured query language designed for managing and manipulating data in relational
database management system
• The sql was invented in the 1970’s based on the relational data model
• It typically includes tables, columns ,datatypes, relationships and constraints.
• Sql is used in most relational database systems like:
>MYSQL
>ORACLE
>SQLITE ETC
. UPDATE example:
- `UPDATE customers SET country='Canada' WHERE name='John Doe';`
SET OPERATIONS:
Allows you to combine the result of two or more queries into single result set.
1.UNION :
• Combines results of two or more queries into a single result set and eliminating
duplicates.
• To retrieve all unique rows from multiple queries
Syntax
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
Example
Suppose you have two tables, Employees and Managers, and you want to get a list of all
unique names from both tables.
SELECT name
FROM Employees
UNION
SELECT name
FROM Managers;
2.UNION ALL:
• Combines the results of two or more queries into a single result set, including
duplicates.
• To retrieve all rows from multiple queries, including duplicates.
Syntax:
SELECT column1, column2, ...
FROM table1
UNION ALL
SELECT column1, column2, ...
FROM table2;
Example:
SELECT name, age
FROM employees
UNION ALL
SELECT name, age
FROM contractors;
3.INTERSECT:
• Returns only the rows that are common to the results set of two or more queries.
• To retrieve only the rows the exists in multiple queries.
Example
Suppose you have two tables, Employees and Managers , and you want to find the
employees who are also managers.
Table: Employees
EmployeeID Name
1 Alice
2 Bob
3 Charlie
Table: Managers
ManagerID Name
2 Bob
3 Charlie
4 David
SELECT Name
FROM Employees
INTERSECT
SELECT Name
FROM Managers;
Result
Name
Bob
Charlie
4.EXCEPT(MINUS):
• Returns only the rows that are present in the result set of the first query but not in the
result set of the second query.
• To retrieve only the rows that exist in one query but not in another.
Example:Suppose you have two tables, Employees and Contractors, and you want to find
all employees who are not contractors.
Table: Employees
EmployeeID Name
1 Alice
2 Bob
3 Charlie
4 David
Table: Contractors
ContractorID Name
2 Bob
4 David
5 Eve
SQL Query using EXCEPT:
SELECT Name
FROM Employees
EXCEPT
SELECT Name
FROM Contractors;
Result:
Name
Alice
Charlie
NULL VALUES
In SQL, some records in a table may not have values for every field, and such fields are
termed as NULL values. These occur when data is unavailable during entry or when the
attribute does not apply to a specific record. To handle such scenarios, SQL provides a
special placeholder value called NULL to represent unknown, unavailable, or inapplicable
data.
• Setting a NULL value is appropriate when the actual value is unknown, or when a
value is not meaningful.
• A NULL value is not equivalent to a value of ZERO if the data type is a number and
is not equivalent to spaces if the data type is a character.
• A NULL value can be inserted into columns of any data type.
• A NULL value will evaluate NULL in any expression.
• Suppose if any column has a NULL value, then UNIQUE, FOREIGN key, and
CHECK constraints will ignore by SQL...
Logical Behavior:SQL uses three-valued logic (3VL): TRUE, FALSE, and UNKNOWN.
Logical expressions involving NULL return UNKNOWN.
• AND: Returns FALSE if one operand is FALSE; otherwise, returns UNKNOWN.
• OR: Returns TRUE if one operand is TRUE; otherwise, returns UNKNOWN.
• NOT: Negates the operand; UNKNOWN remains UNKNOWN.
Example:
SUBQUERIES:
Subqueries in SQL are queries nested inside another SQL query.
They're used to perform intermediate calculations or fetch results that the outer query
depends on.
Types of Subqueries:
1. Single Row Subquery: Returns only one row.
EXAMPLE: consider two tables:
EMPLOYEES
DEPARTMENT
4. Correlated Subquery: Refers to columns in the outer query. Executes once for every row in
the outer query.
SELECT e1.name FROM employees e1
WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e1.department_id =
e2.department_id);
OUTPUT:
3.DELETE:
JOINED RELATIONS :
PL/SQL: