0% found this document useful (0 votes)
11 views22 pages

Unit 3 DB

The document provides an overview of SQL, its key features, and its importance in database management systems. It covers SQL syntax, commands, set operations, NULL values, subqueries, and data definition language (DDL). Additionally, it emphasizes SQL's role in modern databases and its integration with programming languages.

Uploaded by

ahanasayeed.786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Unit 3 DB

The document provides an overview of SQL, its key features, and its importance in database management systems. It covers SQL syntax, commands, set operations, NULL values, subqueries, and data definition language (DDL). Additionally, it emphasizes SQL's role in modern databases and its integration with programming languages.

Uploaded by

ahanasayeed.786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1 Database management system

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

Key Features of SQL


1.Declarative Language:Users specify what they want to retrieve or modify without detailing
how to execute it
Example: SELECT name, age FROM employees WHERE age > 30;
2. Standardized Language:SQL is an ANSI/ISO standard, making it widely supported across
various RDBMS, including Oracle, MySQL, SQL Server, and PostgreSQL.
3. Rich Functionalities:Data Definition Language (DDL): Defines database schema.
Example: CREATE, ALTER, DROP.
Data Manipulation Language (DML): Manipulates data within tables.
Example: SELECT, INSERT, UPDATE, DELETE.
Data Control Language (DCL): Controls user permissions.
Example: GRANT, REVOKE.
Transaction Control Language (TCL): Manages database transactions.
Example: COMMIT, ROLLBACK, SAVEPOINT.
Importance of SQL in DBMS
1. Standard Query Language:SQL provides a unified way to interact with relational
databases, ensuring consistency and portability across systems.
2. Ease of Use:SQL's declarative syntax makes it accessible to users with minimal
programming experience.

Roopa P C lecture AGB college Davangere


2 Database management system

3. Powerful Data Operations:SQL. supports complex queries, aggregation, filtering, joins,


and transactions for robust data manipulation and retrieval.
4. Integration: SQL can be integrated with programming languages like Python, Java, and C#
to build data-driven applications.
5. Adaptability:Modern SQL engines support advanced features like window functions,
recursive queries, and JSON handling, extending its utility.
SQL in Modern DBMS
SQL remains central to modern databases and is the backbone of many data storage and
processing systems. Despite the rise of NoSQL databases for specific use cases, SQL
continues to dominate due to its robustness, simplicity, and wide adoption.

BASIC STRUCTURE OF SQL:


Here is the basic structure of SQL:
# SQL Syntax:
1. Commands: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER
2. Clauses: WHERE, FROM, GROUP BY, HAVING, ORDER BY
3. Functions: SUM, COUNT, AVG, MAX, MIN
# SQL Statement Structure:
1. SELECT statement: retrieves data from a database table.
- `SELECT` column1, column2, ...
- `FROM` tablename: specifies the tables to retrieve data from condition.
- `WHERE` condition: filter data based on the condition.
- `GROUP BY`: groups data by one or more columns
`GROUP BY` column1, column2, ...
- `HAVING` condition: filters grouped data by one or more columns.
- ORDER BY` :sorts data in ascending or descending order.
`ORDER BY` column1, column2, ...
SELECT example:
- `SELECT * FROM customers WHERE country='USA';`

2. INSERT statement: Adds new data to a database table.


`INSERT INTO` tablename (column1, column2, ...)
`VALUES` (value1, value2, ...)
. INSERT example:
`INSERT INTO customers (name, email, country) VALUES ('John Doe',
'[email protected]', 'USA');`

3. UPDATE statement: Modifies existing data in a database table.


- `UPDATE` tablename
`SET` column1 = value1, column2 = value2, ...
`WHERE` condition

Roopa P C lecture AGB college Davangere


3 Database management system

. UPDATE example:
- `UPDATE customers SET country='Canada' WHERE name='John Doe';`

4. DELETE statement: Delete data from a database table.


`DELETE FROM` tablename
`WHERE` condition
. DELETE example:
`DELETE FROM customers 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, ...

Roopa P C lecture AGB college Davangere


4 Database management system

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

SQL Query Using INTERSECT

Roopa P C lecture AGB college Davangere


5 Database management system

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

Roopa P C lecture AGB college Davangere


6 Database management system

FROM Contractors;
Result:
Name
Alice
Charlie

Roopa P C lecture AGB college Davangere


7 Database management system

Roopa P C lecture AGB college Davangere


8 Database management system

Roopa P C lecture AGB college Davangere


9 Database management system

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.

• Importance of NULL Value


It is essential to understand that a NULL value differs from a zero or an empty string.
A NULL value represents missing or undefined data. Since it is often not possible to
determine which interpretation applies, SQL, treats all NULL values as distinct and does not
distinguish between them. Typically, it can have one of three interpretations:
1. Value Unknown: The value exists but is not known,
2. Value Not Available: The value exists but is intentionally withheld.
3. Attribute Not Applicable: The value is undefined for a specific record.

Principles of NULL values

• 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...

Roopa P C lecture AGB college Davangere


10 Database management system

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:

Roopa P C lecture AGB college Davangere


11 Database management system

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.

A subquery is also known as a:


• Nested query
• Inner query
• Inner select
These terms are used interchangeably and refer to a query placed inside another SQL query,
usually inside SELECT, FROM, or WHERE clauses.
The query that contains the subquery is called the outer query or main query.

Types of Subqueries:
1. Single Row Subquery: Returns only one row.
EXAMPLE: consider two tables:
EMPLOYEES

DEPARTMENT

Roopa P C lecture AGB college Davangere


12 Database management system

SELECT name FROM employees


WHERE salary = (SELECT MAX(salary) FROM employees);
OUTPUT:

2. Multiple Row Subquery: Returns multiple rows.


SELECT name FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE
department_name IN( 'HR',’IT’);
OUTPUT:

3. Multiple Column Subquery: Returns more than one column.


SELECT name FROM employees
WHERE (department_id, salary) IN
(SELECT department_id, MAX(salary) FROM employees GROUP BY department_id);
OUTPUT:

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:

Roopa P C lecture AGB college Davangere


13 Database management system

5. Nested Subquery: A subquery inside another subquery.


CONSIDER ANOTHER TABLE LOCATION:

DEPARTMENT WITH LOCATION:

SELECT name FROM employees


WHERE department_id = (SELECT department_id FROM departments
WHERE location_id = (SELECT location_id FROM locations WHERE city = 'Mumbai'));
OUTPUT:

Where You Can Use Subqueries:


In SELECT, FROM, WHERE, or HAVING clauses.
Want a specific example with sample data or want me to explain one of these in detail?

Roopa P C lecture AGB college Davangere


14 Database management system

Roopa P C lecture AGB college Davangere


15 Database management system

Roopa P C lecture AGB college Davangere


16 Database management system

3.DELETE:

JOINED RELATIONS :

Roopa P C lecture AGB college Davangere


17 Database management system

Roopa P C lecture AGB college Davangere


18 Database management system

Roopa P C lecture AGB college Davangere


19 Database management system

.DATA DEFINITION LANGUAGE(DDL):


• It is used to define database structure or pattern.
• It is used to create schema, tables, indexes, constraints, etc. in the
database.
• DDL is used to store the information metadata like the numbers of the
tables and schemas, their names indexes, columns in each table,
constraints etc.
DDL COMMANDS:
• CREATE: Creates the tables.
• ALTER: Alter the structure of the table.
• DROP: To delete the entire table.
• TRUNCATE: To remove all the records from the table (truncate
table branch ;).
• RENAME: To rename the table.
• COMMENT: To comment on the data dictionary.

ALTER TABLE BRANCH RENAME COLUMN Branch_id TO


B_id;

Roopa P C lecture AGB college Davangere


20 Database management system

PL/SQL:

Roopa P C lecture AGB college Davangere


21 Database management system

Roopa P C lecture AGB college Davangere


22 Database management system

Roopa P C lecture AGB college Davangere

You might also like