STRUCTURED
QUERY
LANGUAGE
-SQL
MODULE-3
DATABASE MANAGEMENT SYSTEMS
• Basic SQL querying (Select and Project)
SYLLABU using WHERE clause
S • Arithmetic & Logical Operations
• SQL functions (Date and Time, Numeric,
String Conversion)
• Creating tables with Relationship
• Implementation of Key and Integrity
Constraints
• Nested Queries, Sub Queries
• Grouping, Aggregation, Ordering
• Implementation of different types of
Joins
• View (Updatable And Non-updatable)
• Relational Set Operations
DATABASE MANAGEMENT SYSTEMS
BASIC
BASIC SQL
SQL QUERYING
QUERYING
Select Operation
This operation is used to select rows from a table
that specifies a given logic, which is called as a
predicate. The predicate is a user defined condition
to select rows of user's choice.
Project Operation
If the user is interested in selecting the values of a
few attributes, rather than selection all attributes of
the Relation, then one should go for PROJECT
Operation.
DATABASE MANAGEMENT SYSTEMS MODULE-3
BASIC
BASIC SQL
SQL QUERYING
QUERYING
• SQL SELECT Syntax for Select Operation
SELECT * FROM table_name;
• SQL SELECT Syntax for Project Operation
SELECT column1, column2, ... FROM
table_name
DATABASE MANAGEMENT SYSTEMS MODULE-3
BASIC
BASIC SQL
SQL QUERYING
QUERYING
SELECT using WHERE clause
• The SQL WHERE clause allows to filter the records
in tables.
• Whether you’re retrieving data, updating records, or
deleting entries from a database, the WHERE clause
plays an important role in defining which rows will
be affected by the query.
• Without it, SQL queries would return all rows in a
table, making it difficult to target specific data.
DATABASE MANAGEMENT SYSTEMS MODULE-3
BASIC
BASIC SQL
SQL QUERYING
QUERYING
• SQL SELECT Syntax with WHERE clause
SELECT * FROM table_name WHERE
column_name operator value;
SELECT column1,column2… FROM
table_name
WHERE column_name operator value;
DATABASE MANAGEMENT SYSTEMS MODULE-3
BASIC
BASIC SQL
SQL QUERYING
QUERYING
• WHERE clause with Operators
SELECT first_name, last_name, salary
FROM employees WHERE salary > 50000;
• WHERE clause with AND, OR
SELECT first_name, last_name, department
FROM employees WHERE
department = 'Sales' AND salary > 50000;
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
• While handling data in databases, we often tend to
perform different kinds of operations to manipulate
and retrieve data.
• An operator in SQL is a reserved keyword or a symbol
that operates up on the operands to perform various
operations to return a result.
• An operand can be defined as the data item or an
argument that is used by an operator to perform different
arithmetic, logical and comparison operations.
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS
• In Structured Query Language, the arithmetic operators
are used to perform mathematical operations on the
numerical values stored in the database tables.
• We can use these operators with the SELECT statement in
SQL. We can also use the WHERE clause in the SELECT
statement for performing operations on particular rows.
• These types of operators are used between two numerical
operands for performing addition, subtraction,
multiplication, and division operations.
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS
The arithmetic operators in SQL are categorized into the
following five types:
• Addition Operator ( + )
• Subtraction Operator ( - )
• Multiplication Operator ( * )
• Division Operator ( / )
• Modulus Operator ( % )
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Addition Operator ( + )
• The SQL Addition Operator performs the addition on
the numerical columns in the table.
• If you want to add the values of two numerical
columns in the table, then you have to specify both
columns as the first and second operand.
• You can also add the new integer value in the value
of the integer column.
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Addition Operator ( + )
• Syntax of Addition Operator
• Addition Operator with WHERE Clause
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Addition Operator ( + )
Implementation of Addition operator in SQL:
Employee_Id Emp_Name Emp_City Emp_Salary Emp_bonus
101 Anuj Ghaziabad 25000 2000
102 Tushar Lucknow 29000 1000
103 Vivek Kolkata 35000 2500
104 Shivam Goa 22000 3000
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Addition Operator ( + )
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Subtraction Operator ( -
)
• Syntax of Subtraction Operator
• Subtraction Operator with WHERE Clause
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Subtraction Operator ( -
)
Implementation
Employee_Id
in SQL:
Emp_Name Emp_City Emp_Salary Emp_Panelty
101 Anuj Ghaziabad 25000 500
102 Tushar Lucknow 29000 1000
103 Vivek Kolkata 35000 700
104 Shivam Goa 22000 500
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Multiplication Operator
(*)
• Syntax
• with WHERE Clause
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Multiplication Operator
(*)
Implementation
Car_Number in SQL:
Car_Name Car_Amount Car_Price
2578 Creta 3 1500000
9258 Audi 2 3000000
8233 Venue 6 900000
6214 Nexon 7 1000000
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Division Operator ( / )
• Syntax
• with WHERE Clause
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Division Operator ( / )
Implementation in SQL:
Car_Number Car_Name Car_Amount Car_Price
2578 Creta 3 1500000
9258 Audi 2 3000000
8233 Venue 6 900000
6214 Nexon 7 1000000
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Modulus Operator ( % )
• Syntax
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
ARITHMETIC OPERATORS- Modulus Operator ( % )
Implementation in SQL:
Student_Id Student_Name Student_Maths Student_English
201 Anuj 30 60
202 Tushar 25 100
203 Vivek 30 90
204 Shivam 40 80
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
• The Logical Operators in SQL perform the Boolean
operations, which give two results True and False.
• These operators provide True value if both operands
match the logical condition.
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
StudentID FirstName LastName Age
1 Atul Mishra 23
2 Priya Kapoor 21
3 Rohan Singhania 21
4 Akanksha Jain 20
5 Vaibhav Gupta 25
DATABASE MANAGEMENT SYSTEMS MODULE-3
ARITHMETIC
ARITHMETIC &
& LOGICAL
LOGICAL OPERATIONS
OPERATIONS
LOGICAL OPERATORS
StudentID FirstName LastName Age
1 Atul Mishra 23
2 Priya Kapoor 21
3 Rohan Singhania 21
4 Akanksha Jain 20
5 Vaibhav Gupta 25
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
• SQL functions are pre-defined functions that
can be used to manipulate and perform
operations on data stored in a database.
• These functions can be used to perform a wide
range of tasks, such as performing
calculations, transforming data,
formatting data, and extracting data.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
Types of SQL Functions
• Date and Time Functions
• Numeric Functions
• String Functions
• Conversion Functions
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
• Handling date and time data in SQL is essential for many
database operations.
• SQL provides a variety of date and time functions that help
users work with date values, perform calculations, and
format them as needed.
• Whether you’re adding intervals to dates, extracting parts
of a date, or formatting the output, mastering SQL date
functions is crucial for working with temporal data in
databases.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
• While working with a database, the format of the data
in the table must be matched with the input data to
insert.
• For storing a date or a date and time value in a
database,SQL offers the following data types:
•.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
1. NOW()
• The NOW() function retrieves the current
date and time in YYYY-MM-DD HH:MI:SS
format.
• Query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
2. CURDATE()
CURDATE() returns the current date in YYYY-MM-DD
format, without the time part.
• Query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
3. CURTIME()
The CURTIME() function returns the current time
in HH:MI:SS format, excluding the date.
• Query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
4. DATE()
Extracts the date part of a date or date/time
expression.
Example: For the below table named ‘Test’.
• Query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
5. EXTRACT()
The EXTRACT() function retrieves a specific part of a DATE,
DATETIME, or TIMESTAMP value, such as the day, year, or second.
• Query:
• Several units can be considered but only some are used such as
MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK,
MONTH, QUARTER, YEAR, etc. And ‘date’ is a valid date
expression.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
6. DATE_ADD()
The DATE_ADD() function allows you to add a specified
time interval to a date.
• Query:
• Where, date – valid date expression, and expr is the number of
intervals we want to add and type can be one of the following:
MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH,
QUARTER, YEAR, etc.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
DATE AND TIME FUNCTIONS
7. DATEDIFF()
Returns the number of days between two dates.
• Query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
• SQL Numeric Functions are essential tools for
performing mathematical and arithmetic
operations on numeric data.
• These functions allow you to manipulate
numbers, perform calculations, and
aggregate data for reporting and analysis
purposes.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
1. ABS() – Absolute Value
The ABS() function returns the absolute value of a
number, which is the number without its sign (i.e., it
converts negative numbers to positive).
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
2. CEIL() or CEILING() – Round Number Up
The CEIL() (or CEILING()) function rounds a number up
to the nearest integer, regardless of whether the
decimal part is greater than or less than 0.5.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
3. FLOOR() – Round Number Down
The FLOOR() function rounds a number down to the
nearest integer, ignoring the decimal part.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
4. ROUND() – Round a Number to a Specified Decimal
Place
The ROUND() function rounds a number to a specified
number of decimal places. It is very useful for financial
calculations or whenever precise rounding is necessary.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
5. TRUNCATE() – Remove Decimal Places
The TRUNCATE() function is used to remove the decimal
portion of a number without rounding. It truncates the
number to the specified number of decimal places.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
6. MOD() – Modulo or Remainder
The MOD() function returns the remainder of a division
operation (i.e., it computes the modulus). This function
is useful for tasks like determining even/odd numbers or
finding remainders in mathematical operations.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
7. POWER() – Raise a Number to the Power of Another
The POWER() function is used to raise a number to the
power of another number. It is often used in
mathematical calculations like compound interest or
growth rate.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
8. SQRT() – Square Root
The SQRT() function returns the square root of a
number. This is useful for mathematical calculations
involving geometry or statistical analysis.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
NUMERIC FUNCTIONS
9. RAND() – Random Number
The RAND() function generates a random floating-point
number between 0 and 1. This function is commonly
used for simulations, lotteries, or generating random
samples.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
STRING FUNCTIONS
• SQL String Functions are powerful tools that allow us
to manipulate, format, and extract specific
parts of text data in our database.
• These functions are essential for tasks like cleaning
up data, comparing strings, and combining text fields.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
STRING FUNCTIONS
1. CONCAT(): Concatenate Strings
The CONCAT() function is used to concatenate
(combine) two or more strings into one string. It is
useful when we want to merge fields like first and last
names into a full name.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
STRING FUNCTIONS
2. CHAR_LENGTH() / CHARACTER_LENGTH(): Find String
Length
The CHAR_LENGTH() or LENGTH() function returns the
length of a string in characters. It’s essential for
validating or manipulating text data, especially when
you need to know how many characters a string
contains.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
STRING FUNCTIONS
3. UPPER() and LOWER(): Convert Text Case
These functions convert the text to uppercase or
lowercase, respectively. They are useful for normalizing
the case of text in a database.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
STRING FUNCTIONS
4. REPLACE(): Replace Substring in String
The REPLACE() function replaces occurrences of a
substring within a string with another substring. This is
useful for cleaning up data, such as replacing invalid
characters or formatting errors.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
STRING FUNCTIONS
5. SUBSTRING() / SUBSTR(): Extract Part of a String
The SUBSTRING() (or SUBSTR()) function is used to
extract a substring from a string, starting from a
specified position. It is especially useful when we need
to extract a specific part of a string, like extracting the
domain from an email address.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
• In SQL data type conversion is important for
effective database management and accurate query
results.
• Data type conversion ensures that data from different
sources or columns can be correctly interpreted and
manipulated, especially when dealing with different
formats like numbers, text, dates, and other data types.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
Types of Data Type Conversion in SQL
• Implicit Data Type Conversion: This is done automatically
by the database management system (DBMS) when SQL
operations involve columns of different data types.
• Explicit Data Type Conversion: This is done by the user,
who specifies the conversion. This is necessary when SQL
cannot automatically convert between data types, or when
more control over the conversion is needed.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
Implicit Data-Type Conversion
• Implicit data type conversion, also known as
automatic type casting, occurs when SQL
automatically converts one data type to another
without requiring any intervention from the user.
• The DBMS does this whenever it detects a need for the
conversion, based on the context of the operation.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
Implicit Data-Type Conversion
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
Explicit Data-Type Conversion
• Explicit data type conversion, or type casting,
occurs when the user explicitly specifies the
conversion of a value from one type to another.
• This is necessary when SQL cannot automatically
infer the correct conversion or when you want to
ensure the data is handled in a specific way.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
Explicit Data-Type Conversion
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS
Explicit Data-Type Conversion
• SQL provides several functions for explicit type
conversion, including:
• TO_CHAR(): Converts numbers or dates to a string.
• TO_NUMBER(): Converts a string to a numeric type.
• TO_DATE(): Converts a string to a date.
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS -Explicit Data-Type
Conversion
1. TO_CHAR Function
• TO_CHAR function is used to typecast a numeric or
date input to a character type with a format model
(optional).
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS -Explicit Data-Type
Conversion
1. TO_CHAR Function
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS -Explicit Data-Type Conversion
1. TO_DATE Function
• The TO_DATE function returns an item of type DATE. Character
strings converted to dates may contain all or just a subset of the
date-time elements comprising a DATE.
• When strings with only a subset of the date time elements are
converted, Oracle provides default values to construct a complete
date.
• .
DATABASE MANAGEMENT SYSTEMS MODULE-3
SQL
SQL FUNCTIONS
FUNCTIONS
CONVERSION FUNCTIONS -Explicit Data-Type Conversion
1. TO_NUMBER Function
• The TO_NUMBER function returns an item of type NUMBER.
• Character strings converted into numbers must be suitably
formatted so that any nonnumeric components are
translated or stripped away with an appropriate format mask.
• .
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
• Relationships in SQL tables define how tables are connected to one
another.
• Building relationships in tables helps to organize and link data across
multiple tables.
• A relationship between two database tables presupposes that one of them
has a foreign key that references the primary key of another table.
• Creating relationships among tables provides efficient data retrieval
and maintenance.
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
• Type of Relationships in SQL
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
• Type of Relationships in SQL
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
One-to-One Relationship
• Definition: Each record in Table A is associated with
one and only one record in Table B, and vice versa.
• Setup: Include a foreign key in one of the tables that
references the primary key of the other table.
• For example: Tables users and user_profiles, where
each user has a single corresponding profile.
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
One-to-One Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
One-to-One Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
One-to-Many Relationship
• Definition: Each record in Table A can be associated with
multiple records in Table B, but each record in Table B is
associated with only one record in Table A.
• Setup: Include a foreign key in the "many" side table
(Table B) that references the primary key of the "one" side
table (Table A).
• For example: Tables departments and employees, where
each department can have multiple employees, but each
employee belongs to one department.
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
One-to-Many Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
One-to-Many Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
Many-to-Many Relationship
• Definition: Each record in Table A can be associated
with multiple records in Table B, and vice versa.
• Setup: Create an intermediate table (also known as a
junction or linking table) that contains foreign keys
referencing both related tables.
• For example: Tables students and courses, where
each student can enroll in multiple courses, and each
course can have multiple students.
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
Many-to-Many Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
Many-to-Many Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
Many-to-Many Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
Self Referencing Relationship
• Definition: A table has a foreign key that references
its primary key.
• Setup: Include a foreign key column in the same
table that references its primary key.
• For example : A table employees with a column
manager_id referencing the same table's
employee_id.
DATABASE MANAGEMENT SYSTEMS MODULE-3
CREATING
CREATING TABLES
TABLES WITH
WITH RELATIONSHIPS
RELATIONSHIPS
Self Referencing Relationship
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
• Constraints can be defined in two ways
1) The constraints can be specified immediately after
the column definition. This is called column-level
definition.
2) The constraints can be specified after all the
columns are defined. This is called table-level
definition.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Primary key
• This constraint defines a column or
combination of columns which uniquely
identifies each row in the table.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Primary key
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Foreign key or Referential Integrity
• This constraint identifies any column referencing the
PRIMARY KEY in another table.
• It establishes a relationship between two columns in
the same table or between different tables.
• For a column to be defined as a Foreign Key, it should
be a defined as a Primary Key in the table which it is
referring.
• One or more columns can be defined as Foreign key.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Foreign key or Referential Integrity
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Foreign key or Referential Integrity
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Foreign key or Referential Integrity
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Unique Key
• This constraint ensures that a column or a group of
columns in each row have a distinct value. A
column(s) can have a null value but the values
cannot be duplicated.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Unique Key
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Not Null Constraint
• This constraint ensures all rows in the table contain a
definite value for the column which is specified as not
null. Which means a null value is not allowed.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF KEY
KEY AND
AND INTEGRITY
INTEGRITY
CONSTRAINTS
CONSTRAINTS
SQL Not Null Constraint
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
NESTED QUERIES
• A nested query is a query that has another query
embedded within it; the embedded query is called a
subquery.
• When writing a query, we sometimes need to express a
condition that refers to a table that must itself be
computed.
• The query used to compute this subsidiary table is a
subquery and appears as part of the main query.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
NESTED QUERIES
• A subquery typically appears within the WHERE clause of
a query.
• Subqueries can sometimes appear in the FROM clause or
the HAVING clause.
• Nested queries are commonly used for filtering data,
performing calculations, or joining datasets
indirectly.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
NESTED QUERIES
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Independent Nested Queries
• In independent nested queries, the execution of the
inner query is not dependent on the outer query.
• The result of the inner query is used directly by the
outer query.
• Operators like IN, NOT IN, ANY, and ALL are commonly
used with this type of nested query.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Independent Nested Queries
Example: Using IN
• Find the S_IDs of students who are enrolled in
the courses ‘DSA’ or ‘DBMS’.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Independent Nested Queries
Step 1: Find the C_IDs of the courses:
• This query retrieves the IDs of the courses
named ‘DSA’ or ‘DBMS’ from the COURSE table.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Independent Nested Queries
Step 2: Use the result of Step 1 to find the
corresponding S_IDs:
The inner query finds the course IDs, and the
outer query retrieves the student IDs associated
with those courses from the STUDENT_COURSE
table
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Correlated Nested Queries
• In correlated nested queries, the inner query
depends on the outer query for its execution.
• For each row processed by the outer query, the
inner query is executed.
• The EXISTS keyword is often used with correlated
queries.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Correlated Nested Queries
Example: Using EXISTS
• Find the names of students who are enrolled in the
course with C_ID = ‘C1’:
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Common SQL Operators for Nested Queries
1. IN Operator
• The IN operator is used to filter rows based on a set of
values returned by a subquery.
• It is commonly used to match a column value against a list
or result set.
• This operator simplifies queries by avoiding the need for
multiple OR conditions.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Common SQL Operators for Nested Queries
Example: Retrieve student names who enrolled in
‘DSA’ or ‘DBMS’:
• This query filters the students enrolled in the
specified courses by chaining multiple nested queries.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Common SQL Operators for Nested Queries
2. NOT IN Operator
• The NOT IN operator excludes rows based on a set of
values from a subquery.
• It is particularly useful for filtering out unwanted results.
• This operator helps identify records that do not match
the conditions defined in the subquery.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Common SQL Operators for Nested Queries
Example: Retrieve student IDs not enrolled in
‘DSA’ or ‘DBMS’:
• This query excludes students who are enrolled in the
courses ‘DSA’ or ‘DBMS’.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Common SQL Operators for Nested Queries
EXISTS
• The EXISTS operator checks for the existence of rows in a
subquery.
• It returns true if the subquery produces any rows, making it
efficient for conditional checks.
• This operator is often used to test for relationships
between tables.
DATABASE MANAGEMENT SYSTEMS MODULE-3
NESTED
NESTED QUERIES,
QUERIES, SUB
SUB QUERIES
QUERIES
Common SQL Operators for Nested Queries
EXISTS
Example: Find student names enrolled in ‘DSA’
• The inner query checks for matching records in the
STUDENT_COURSE table, and the outer query returns
the corresponding student names.
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
GROUPING
• The GROUP BY clause in SQL is used to arrange
identical data into groups based on one or more
columns.
• Once data is grouped, aggregate functions can be
used to summarize the information within those
groups.
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
AGGREGATION
• Aggregate functions perform calculations
on multiple rows of data and return a single result.
• Common
functions include COUNT(), SUM(), AVG(), MIN(),
and MAX().
• These help us get summaries like totals, averages,
and counts for different categories or groups.
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
ORDERING
• Ordering is used to sort the result set of a query by one or more
columns. The ORDER BY clause is used for this purpose.
• The command enables you to define how data is ordered in the
target table.
• Additionally, you can specify whether data is sorted in ascending
or descending order.
• By default, ORDER BY delivers results in ascending order. If you
prefer results in descending order, you can use the optional
keyword DESC.
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
SYNTAX
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
EXAMPLE
Let's say we have a table named sales with the following
columns: id, product_id, quantity, and price.
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
EXAMPLE
To find the total quantity sold and the total revenue for
each product, you can use the following query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
EXAMPLE
To find the total quantity sold and the total revenue for
each product, you can use the following query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
EXAMPLE
To find the total quantity sold and the total revenue for
each product, you can use the following query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
GROUPING,
GROUPING, AGGREGATION,
AGGREGATION,
ORDERING
ORDERING
EXAMPLE
To find the total quantity sold and the total revenue for
each product, you can use the following query:
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
JOINS
• SQL joins are the foundation of database management systems,
enabling the combination of data from multiple tables
based on relationships between columns.
• It can access data from multiple tables simultaneously
using common key values shared across different tables.
• Joins allow efficient data retrieval, which is essential for
generating meaningful observations and solving complex
business queries.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
JOINS- EXAMPLE:
Consider the two tables, Student and StudentCourse, which share a common column ROLL_NO.
Using SQL JOINS, we can combine data from these tables based on their relationship, allowing us
to retrieve meaningful information like student details along with their enrolled courses.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
JOINS- EXAMPLE:
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
There are many types of Joins in SQL. Depending on the
use case, we can use different type of SQL JOIN clause.
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
• Natural Join
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
• INNER JOIN: Returns records that have matching
values in both tables
• LEFT JOIN: Returns all records from the left table,
and the matched records from the right table
• RIGHT JOIN: Returns all records from the right table,
and the matched records from the left table
• FULL JOIN: Returns all records when there is a match
in either left or right table
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
INNER JOIN
• The INNER JOIN keyword selects all rows from both
the tables as long as the condition is satisfied.
• This keyword will create the result-set by combining
all rows from both the tables where the condition
satisfies i.e value of the common field will be the
same.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
INNER JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL - INNER JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
LEFT JOIN
• LEFT JOIN returns all the rows of the table on the left side
of the join and matches rows for the table on the right
side of the join.
• For the rows for which there is no matching row on the right
side, the result-set will contain null.
• LEFT JOIN is also known as LEFT OUTER JOIN.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL-LEFT JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL-LEFT JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
RIGHT JOIN
• RIGHT JOIN returns all the rows of the table on the right
side of the join and matching rows for the table on the
left side of the join.
• It is very similar to LEFT JOIN for the rows for which there is no
matching row on the left side, the result-set will contain null.
• RIGHT JOIN is also known as RIGHT OUTER JOIN.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL- RIGHT JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL- RIGHT JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
FULL JOIN
• FULL JOIN creates the result-set by combining results of
both LEFT JOIN and RIGHT JOIN.
• The result-set will contain all the rows from both tables.
• For the rows for which there is no matching, the result-set
will contain NULL values.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL-FULL JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS NAME COURSE_ID
TYPES OF JOINS IN SQL-FULL JOIN
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
NATURAL JOIN
• Natural join can join tables based on the common
columns in the tables being joined.
• A natural join returns all rows by matching values
in common columns having same name and
data type of columns and that column should be
present in both tables.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL
NATURAL JOIN
• Both table must have at least one common column
with same column name and same data type.
• The two table are joined using Cross join.
• DBMS will look for a common column with same
name and data type. Tuples having exactly same
values in common columns are kept in result.
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL- NATURAL JOIN
DATABASE MANAGEMENT SYSTEMS MODULE-3
IMPLEMENTATION
IMPLEMENTATION OF
OF DIFFERENT
DIFFERENT
JOINS
JOINS
TYPES OF JOINS IN SQL- NATURAL JOIN
Problem: Find all Employees and their respective
departments.
Solution Query: (Employee) ? (Department)
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
• A set operator in SQL is a keyword that lets you
combine the results of two queries into a
single query.
• Sometimes when working with SQL, you’ll have a
need to query data from two more tables.
• But instead of joining these two tables, you’ll
need to list the results from both tables in a
single result, or in different rows.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET SET OPERATIONS
OPERATIONS
The different set operators are:
• UNION
• UNION ALL
• MINUS
• INTERSECT
• EXCEPT
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
• General syntax for using Set Operators
• It uses two (or more) SELECT queries, with a set operator
in the middle.
• When selecting your columns, the number of columns
needs to match between queries, and the data
type of each column needs to be compatible.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
UNION: Combining Results
• The UNION keyword or set operator will allow you to
combine the results of two queries.
• It removes any duplicate results and shows you the
combination of both.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
UNION: Combining Results
• But looking at this, we can’t tell which record comes from
each table.
• We can add a static value to each query to indicate which
table it came from, or what type of record it is.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
UNION: Combining Results
• But looking at this, we can’t tell which record comes from
each table.
• We can add a static value to each query to indicate which
table it came from, or what type of record it is.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
UNION ALL
• The UNION ALL set operator also combines the results
from two queries.
• It’s very similar to UNION, but it does not remove
duplicates.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
MINUS or EXCEPT: Finding Results That Are Missing
• The MINUS set operator will return results that are found in
the first query specified that don’t exist in the second
query.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
INTERSECT: Showing Common Results
• The INTERSECT keyword allows you to find results that
exist in both queries. Two SELECT statements are needed,
and any results that are found in both of them are returned
if INTERSECT is used.
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
DATABASE MANAGEMENT SYSTEMS MODULE-3
RELATIONAL
RELATIONAL SET
SET OPERATIONS
OPERATIONS
Combining two or more Set Operators
DATABASE MANAGEMENT SYSTEMS MODULE-3
END OF MODULE-3
DATABASE MANAGEMENT SYSTEMS MODULE-3