Dbms Module-2 (Mmc103)
Dbms Module-2 (Mmc103)
MODULE-2
Relational Algebra and Overview of the SQL Query Language
Introduction to Relational Algebra
Relational Algebra is a formal system for manipulating and querying relational databases. It
provides a set of operations that take one or more relations (tables) as input and produce a new
relation as output. Relational algebra is the theoretical foundation of relational databases and is
used for querying and managing data.
It was introduced by Edgar F. Codd in 1970 as part of his relational model for databases. The
main purpose of relational algebra is to offer a theoretical framework for querying databases,
ensuring that operations on data follow a set of principles that make querying efficient and logical.
Basic Concepts
In relational algebra, operations are used to manipulate relations (tables). These operations can be
classified into two types based on the number of relations they operate on: unary and binary
operations.
1. Unary Operations
Unary operations in relational algebra are operations that operate on only one relation at a time.
These operations are applied to a single relation and produce a new relation as the result.
a. SELECTION (σ):
o The selection operation is used to filter rows from a relation based on a given
condition.
o It selects a subset of rows that satisfy a specified predicate.
o Syntax: σ <selection condition> (Relation)
where,
The symbol σ(sigma) is used to denote the SELECT operator
The selection condition is a Boolean (conditional) expression specified on the attributes of
relation R
Tuples that make the condition true are selected
appear in the result of the operation
Tuples that make the condition false are filtered out
discarded from the result of the operation
Examples:
1. Select the EMPLOYEE tuples whose department number is 4.
σDNO = 4 (EMPLOYEE)
3. Select the tuples for all employees who either work in department 4 and make over
$25,000 per year, or work in department 5 and make over $30,000
σ (Dno=4 AND Salary>25000) (EMPLOYEE)
b. PROJECTION (π):
o The projection operation is used to select specific columns from a relation.
o It removes duplicate rows and keeps only the specified attributes.
o Syntax: π <attribute list> (Relation)
where
symbol π (pi)used to represent the PROJECT operation
<attributelist> - desired sublist of attributes from the attributes of relation R.
The result of the PROJECT operation has only the attributes specified in <attribute
list> in the same order as they appear in the list. Hence, its degree is equal to the
number of attributes in <attribute list>
Example:
π Name,Age(Employees)
would to the following SQL query:
SELECT Name, Age FROM Employees;
This means we are selecting the Name and Age columns from the Employees relation
and removing any duplicate rows.
Result of Projection:
Name Age
Alice 30
Bob 25
Charlie 28
David 35
Eve 29
c. RENAMING (ρ):
o The renaming operation is used to rename the attributes of a relation or even the
entire relation.
o This is useful when we need to avoid ambiguity or for clarity in further operations.
o Syntax: ρ_new_relation_name(new_attribute1, new_attribute2)(Relation)
Where:
Example:
2. Binary Operations
Binary operations in relational algebra involve two relations. These operations combine, compare,
or relate two different relations to produce a new relation as the result.
A. UNION (∪):
The union operation combines the tuples of two relations, eliminating duplicate
tuples. Both relations must have the same number of attributes and corresponding
attributes must have the same domain.
Syntax: Relation1 ∪ Relation2
B. INTERSECTION (∩):
The intersection operation returns the tuples that appear in both relations. Similar to
union, the two relations must have the same number of attributes.
Syntax: Relation1 ∩ Relation2
C. DIFFERENCE/MINUS (−):
The difference operation returns the tuples that are in the first relation but not in the
second relation.
Syntax: Relation1 − Relation2
The Cartesian product operation combines every tuple from the first relation with every
tuple from the second relation. This results in a relation that contains all possible pairs
of tuples from the two relations.
Syntax: Relation1 × Relation2
Employees: Departments:
E. JOIN (⨝):
In relational algebra, the join operation combines two relations based on a common attribute
or condition. This operation allows you to merge tuples from different relations in a
meaningful way, creating new relations. There are several types of joins in relational algebra,
each serving different purposes for combining data from multiple relations.
A Theta Join is the most general type of join. It allows you to combine tuples from two relations
based on a condition (θ) that can be any comparison operator, such as =, >, <, !=, etc.
The condition can be any boolean expression that compares attributes from the two relations, not
necessarily equality.
Example:
Employees: Departments:
102 Bob 40 2 35
An Equi Join is a special case of the Theta Join where the condition is specifically an equality
condition (=) between attributes from both relations.
The condition used in an equi join is always equality (=) between attributes from the two
relations.
Example:
Employees: Departments:
Employees.Department_ID = Departments.Department_ID:
101 Alice 1 HR
102 Bob 2 IT
103 Charlie 1 HR
A Natural Join is a type of Equi Join, but with two key differences:
1. It automatically joins the two relations on all attributes with the same name.
2. It removes duplicate columns that are common between the two relations.
The condition is based on equality of all common attributes between the two relations, and it
automatically eliminates duplicate columns in the result.
Example:
Employees: Departments:
101 Alice 1 HR
102 Bob 2 IT
103 Charlie 1 HR
4. Outer Joins
Outer joins are extensions of the join operation that include unmatched tuples from one or both
relations in the result. There are three types of outer joins:
A left outer join returns all tuples from the left relation (Relation1), and the matching tuples from
the right relation (Relation2). If there is no match, the result will contain NULL values for
attributes of the right relation.
Employees: Departments:
A right outer join returns all tuples from the right relation (Relation2), and the matching tuples
from the left relation (Relation1). If there is no match, the result will contain NULL values for
attributes of the left relation.
101 Alice 1 HR
102 Bob 2 IT
NULL NULL 3 IT
Let's combine selection (σ) and projection (π) operations with the binary operations (union,
intersection, difference) in relational algebra to see how these can be applied together. This will
allow us to filter data (selection) and choose specific columns (projection) while combining or
manipulating relations.
Selection (σ) is used to filter rows based on a condition, and Union (∪) combines all tuples from
two relations. When combined, you can filter data first, then perform a union operation between
two filtered relations.
Example:Let's assume you have two relations:
Employees: Contractors:
104 David 35 HR
103 Charlie 28 IT
The intersection (∩) operation returns tuples that are common in both relations. By combining it
with selection (σ), you can first filter relations, then find the intersection of the filtered results.
Example: Let’s select employees older than 30 and contractors from the HR department, and then
find the common tuples between these two relations.
The difference (−) operation returns tuples that are in the first relation but not in the second. By
applying selection first, you can filter the data before performing the difference.
Example: Let’s find employees who are older than 30 but not contractors in the IT department.
Relational Algebra: σ(Age > 30)(Employees) − σ(Department = 'IT')(Contractors)
Concept of Division
In simple terms, division is used when you want to retrieve rows from one relation that are associated with
all tuples in another relation. It is applied between two relations: one representing the dividend (the relation
from which you want to select) and the other representing the divisor (the relation whose tuples must be
fully matched with those in the dividend).
The result of a division operation is a new relation that contains all tuples from the dividend relation that are
associated with every tuple in the divisor relation.
Syntax: Dividend ÷ Divisor
Where:
Dividend is the relation containing the tuples you are interested in.
Divisor is the relation whose tuples should be matched against all tuples in the dividend.
1. Projection: First, you project the attributes from the dividend that are not present in the
divisor. These are the attributes you are interested in finding.
2. Difference: Then, you find the tuples in the dividend that have all the corresponding values
in the divisor.
Example of Division
Let's take an example to better understand how division works.
Employee_ID Department_ID
1 D1
1 D2
2 D1
2 D2
3 D1
Relation 2 (Departments): This relation contains the departments we are interested in.
Department_ID
D1
D2
Problem: Find all employees who work in all departments (i.e., both D1 and D2).
Step 1: Project the attributes of the Employee_Department relation that are not in the
Departments relation. Here, the only attribute we care about is Employee_ID.
Step 2: Divide Employee_Department by Departments.
Employee_ID
1
2
Explanation:
Tuple Relational Calculus (TRC) is a form of relational calculus where queries are
expressed in terms of tuples (rows) of a relation. It uses variables that represent tuples and
specifies conditions (predicates) that must be satisfied for the tuples in the result set.
Syntax: {T | P (T)}
Where:
A tuple variable T can range over a relation (table), and P(T) is a logical expression that must be
satisfied for the tuples that appear in the result.
Suppose we want to find the names of employees who are older than 30.
Explanation:
Name
Bob
David
Domain Relational Calculus (DRC) is another form of relational calculus, but instead of
working with entire tuples, it works with domain variables (values of attributes). DRC
queries specify what values from the domain (column values) satisfy a given condition.
Where:
attribute1, attribute2, ..., attributeN are domain variables (representing values in the
relation).
P(attribute1, attribute2, ..., attributeN) is the predicate (condition) that specifies the
values satisfying the query.
Example:
Let's use the same Employees relation, and we want to find the names and department
IDs of employees who are older than 30.
Name Department_ID
Bob 2
David 2
SQL (Structured Query Language) is the standard language for managing and manipulating
relational databases. It is a declarative language, meaning users specify what data they want,
rather than how to get it. SQL allows users to interact with databases by performing operations
such as data retrieval, insertion, updating, deletion, and database management.
Querying databases
Inserting, updating, and deleting records
Creating and modifying database structures (tables, schemas)
Creating views, triggers, and stored procedures
KEY COMPONENTS:
SQL supports various data types that define what kind of data a column in a table can store.
Different data types help in maintaining the integrity of data by ensuring that only appropriate
types of values are stored in each column. These data types are typically categorized into numeric
types, character types, date and time types, boolean types, and binary types.
These types are used to store numbers, both integers and floating-point numbers.
INT (Integer): Used to store whole numbers (positive, negative, or zero) without decimals.
o Example: INT is typically used for storing employee IDs or age.
o Range: Typically from -2,147,483,648 to 2,147,483,647 (depends on the database
system).
FLOAT: Used to store approximate numeric values with floating decimal points.
o Example: Used to store scientific data or measurements with decimals, like 3.14 or
-0.006.
o Range: Typically stores up to 7 decimal digits.
DECIMAL or NUMERIC: Stores exact numeric values with a specified precision and
scale. Unlike FLOAT, it ensures precise calculations.
o Example: Used to store financial data, like monetary values (DECIMAL(10, 2)
means 10 digits in total, with 2 digits after the decimal point).
o Syntax: DECIMAL(precision, scale) where precision is the total number of digits,
and scale is the number of digits to the right of the decimal.
DOUBLE (Double Precision): Used to store approximate numeric values, with a larger
precision than FLOAT.
o Example: Used for precise scientific calculations that require more decimal places
than FLOAT.
These types are used to store text data, such as names, descriptions, and other strings.
CHAR (Fixed Length): Used to store fixed-length strings. If the string is shorter than the
defined length, it will be padded with spaces.
o Example: CHAR(10) will always store 10 characters. If the string is shorter, the
remaining space will be filled with spaces.
o Use Case: Ideal for storing fixed-length values, like state abbreviations (e.g., NY,
CA).
VARCHAR (Variable Length): Used to store variable-length strings. It stores only the
characters that are actually used, saving space.
o Example: VARCHAR(100) can store a string of any length up to 100 characters.
o Use Case: Used for text fields like names, addresses, and email addresses.
TEXT: Used to store large amounts of text. Unlike CHAR or VARCHAR, the TEXT type
is used for storing long strings, such as large descriptions or articles.
o Example: Used for storing comments or large textual content.
o Use Case: Ideal for storing long notes, articles, or descriptions that exceed the
limits of VARCHAR.
These types are used to store binary data, such as images, files, or multimedia.
BLOB (Binary Large Object): Used to store large binary objects like images, audio files,
or any kind of file data.
o Example: Storing an image file or a PDF document.
o Use Case: Ideal for storing files that need to be retrieved and used within the
application.
VARBINARY: Similar to BLOB, but with a variable length. Used to store binary data
where the length of the data can vary.
o Example: Used to store encrypted data or file paths in binary format.
SQL CATEGORIES:
DDL commands are used to define and manage the structure of the database objects (such as
tables, schemas, and indexes). They do not manipulate the data itself, but rather the schema and its
components.
A. CREATING A DATABASE
The CREATE DATABASE statement is a foundational SQL command used to create new
databases in SQL-based Database Management Systems (DBMS), including MySQL,
PostgreSQL, SQL Server, and others. Understanding how to use this command effectively is
crucial for developers, database administrators, and anyone working with relational databases.
Syntax:
CREATE DATABASE database_name;
Example:
CREATE DATABASE College;
1. Naming Conventions: Use unique, descriptive names without spaces (use underscores).
Example: my_database.
2. Max Length: Database names are typically limited to 128 characters.
3. Permissions: You need administrative privileges to create a database.
4. Case Sensitivity: Most SQL systems are case-insensitive, but it’s best to use lowercase
consistently.
5. Cross-DB Compatibility: While the syntax is similar across systems (e.g., MySQL,
PostgreSQL, SQL Server), some systems have specific options (like character set or
storage engine).
In MongoDB, you don’t need to explicitly create a database; just use the use command to switch
to a database, and MongoDB will create it when you insert data.
Syntax:
use database_name;
Example:
use College;
Example:
use College;
CREATE TABLE
Steps:
1. Define the table's structure: List the columns with their data types.
2. Specify constraints: Indicate primary keys, unique fields, or not-null constraints.
B. ALTER TABLE
The most common use of ALTER is to modify a table. Here's how you can use it in different
scenarios
ADD A COLUMN
You can add one or more columns to an existing table using the ALTER TABLE statement with
the ADD keyword.
This adds a new column named email with a VARCHAR(100) data type to the students
table.
DROP A COLUMN
You can remove a column from a table using the ALTER TABLE statement with the DROP COLUMN
keyword.
You can change the data type or size of an existing column using the ALTER TABLE statement with
the MODIFY or CHANGE keyword (depending on the DBMS).
This changes the age column data type to INT (if it was something else previously).
ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_datatype;
Example (POSTGRESQL):
RENAME A COLUMN
You can rename an existing column using the ALTER TABLE statement with the RENAME COLUMN
keyword.
Syntax (PostgreSQL):
Example:
RENAME A TABLE
You can rename a table using the ALTER TABLE statement with the RENAME TO keyword.
Important Considerations:
Irreversible: Dropping a database will remove all data and structures associated with it, so
ensure that you have a backup if necessary.
Permissions: You need administrative privileges to drop a database.
Make sure that no active connections are using the database, as some systems may prevent
dropping a database if it is in use.
Drop table
To drop a table in SQL, you use the DROP TABLE statement. This command permanently
deletes the table along with all the data and structure it contains.
Important Considerations:
Irreversible: Dropping a table will permanently remove the table and all its data. Make
sure to back up the data if needed.
Permissions: You need appropriate privileges to drop a table.
Dependencies: If there are foreign keys or other dependencies (e.g., views, triggers), you
may need to drop them first or adjust them before dropping the table.
D. TRUNCATE IN SQL
The TRUNCATE command in SQL is used to remove all rows from a table quickly, while keeping
the table structure intact. This means that the table itself remains available for use, but all the data
within it is deleted.
Example:If we have a table students and we want to remove all records from it, we would use:
2.DML COMMANDS
DML (Data Manipulation Language) commands in SQL are used to manipulate and manage
data within existing database tables. These commands are focused on inserting, updating,
deleting, and querying the data. DML operations are typically non-structural and affect only the
data in the table, not the schema or the table structure itself.
1. INSERT
2. SELECT
3. UPDATE
4. DELETE
1. INSERT
The INSERT command is used to add new rows of data into a table.
Syntax:
Example:
This adds a new row to the students table with the provided values.
2. SELECT
The SELECT command is used to retrieve data from one or more tables. You can use different
clauses like WHERE, ORDER BY, GROUP BY, etc., to filter, sort, or group the results.
Syntax:
Example:
SELECT first_name, last_name, age FROM students WHERE age > 21;
This retrieves the first_name, last_name, and age of students whose age is greater than
21.
3. UPDATE
The UPDATE command is used to modify the existing data in a table. It allows you to change values
in one or more columns for specific rows.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE students
SET age = 23
WHERE student_id = 1;
Note: Always use the WHERE clause to specify which records should be updated. Without it, all
rows in the table will be updated.
4. DELETE
The DELETE command is used to remove rows from a table. You can use the WHERE clause to
specify which rows to delete.
Syntax:
Example:
2. SELECT: To get the names and ages of students who are 22 or older:
SELECT first_name, last_name, age FROM students WHERE age >= 22;
UPDATE students
SET age = 22
WHERE student_id = 3;
CLAUSES IN SQL
In SQL, clauses are the components of a query that define its structure and functionality. They
specify the conditions, filters, sorting, grouping, and relationships between data. Here are the main
SQL clauses
1. SELECT Clause
2. FROM Clause
3. WHERE Clause
The WHERE clause in SQL filters rows based on conditions. It supports comparison operators
(=, >, <, etc.), logical operators (AND, OR, NOT), and special operators like IN, BETWEEN,
LIKE, and IS NULL for advanced filtering.
Example:
SELECT *
FROM employees
WHERE salary > 50000;
4. GROUP BY Clause
The GROUP BY clause in SQL is used to group rows that have the same values in specified
columns. It is often used with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and
MIN() to summarize data within each group.
The GROUP BY clause must be used after the WHERE clause and before the ORDER BY
clause. It can be combined with the HAVING clause to filter grouped data based on conditions.
Only columns listed in the GROUP BY clause or used in aggregate functions can appear in the
SELECT statement.
Syntax
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
Output: Groups employees by department and counts the number of employees in each.
Output: Groups rows by department and calculates the average salary for each.
5. HAVING Clause
The HAVING clause in SQL is used to filter grouped data after applying the GROUP BY
clause. Unlike the WHERE clause, which filters rows before grouping, the HAVING clause
filters groups after aggregation. It is often used with aggregate functions like COUNT(),
SUM(), and AVG() to apply conditions to grouped results.
Syntax
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;
6. ORDER BY Clause
The ORDER BY clause in SQL is used to sort the result set in either ascending (ASC) or
descending (DESC) order. By default, the sorting is in ascending order. It can be applied to one or
more columns to arrange the data in a specific sequence.
Syntax
SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];
SET OPERATIONS
Set operations in SQL are used to combine the results of two or more SELECT queries. They allow
you to perform operations like union, intersection, and difference on result sets. The most common
set operations in SQL are:
1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT (or MINUS in some databases)
Example Tables
1. UNION
The UNION operator combines the results of two SELECT queries and removes duplicates. The
columns must have the same names and data types in both queries.
Alice, Charlie, and Eve appear in both tables, but only one instance of each is included in
the result.
2. UNION ALL
The UNION ALL operator combines the result sets of two queries but does not remove duplicates.
SELECT EmployeeName
FROM Employees Name
UNION ALL Alice
SELECT CustomerName Bob
FROM Customers; Charlie
David
Eve
Alice
John
Charlie
Helen
Eve
Notice that Alice, Charlie, and Eve appear twice, as UNION ALL doesn't eliminate
duplicates.
3. INTERSECT
The INTERSECT operator returns only the rows that are present in both result sets.
Only Alice, Charlie, and Eve appear in both the Employees and Customers tables, so
they are returned.
The EXCEPT operator returns the rows from the first query that do not exist in the second query. It
removes any common rows.
Only Bob and David appear in the Employees table but not in the Customers table, so they
are returned.
AGGREGATE FUNCTIONS
Aggregate functions in SQL are used to perform calculations on a set of values and return a single
value. These functions are commonly used with the GROUP BY clause to summarize or group
data, but they can also be used without grouping.
1. COUNT()
2. SUM()
3. AVG()
4. MIN()
5. MAX()
1. COUNT(): The COUNT() function returns the number of rows that match a specified condition
or the total number of rows in a column.
Syntax:
COUNT(column_name)
Example:
SaleID Product Amount
1 A 100
2 B 150
3 A 200
4 C 120
5 A 180
If you want to count how many sales of product "A" there were:
2. SUM(): The SUM() function returns the total sum of a numeric column.
Syntax:
SUM(column_name)
Example:
To find the total amount of sales in the Sales table:
TotalAmount
SELECT SUM(Amount) AS TotalAmount
FROM Sales;
750
3. AVG(): The AVG() function returns the average value of a numeric column.
Syntax:
AVG(column_name)
Example:
To find the average sales amount:
Syntax:
MIN(column_name)
Example:
To find the minimum sales amount:
MIN() works on numeric, date, or text columns and returns the smallest value.
Syntax:
MAX(column_name)
Example:
To find the maximum sales amount:
MaxAmount
SELECT MAX(Amount) AS MaxAmount 200
FROM Sales;
TRIGGERS
A trigger in SQL is a special kind of stored procedure that automatically executes (or fires) in
response to certain events on a table or view. Triggers can be set to run before or after an
INSERT, UPDATE, or DELETE operation is performed on a table.
Trigger Types:
EXAMPLE
DELIMITER //
Create trigger T
after insert on voter
for each row
begin
update constituency set no_of_voters=no_of_voters+1 where
cons_id=new.cons_id;
end
//
insert into voter values(300,'suresh',25,'chikodi',111,121);
//
select *from constituency;
//
STORED PROCEDURE
A stored procedure in SQL is a set of precompiled SQL statements that are stored together in a
database. Stored procedures are used to perform operations such as querying data, updating
records, or executing complex logic. They are stored within the database and can be executed
multiple times, providing efficiency, security, and code reusability.
Create a stored procedure to insert the tuple into the voter table by checking the voter age. If
voter‟s age is at least 18 years old, then insert the tuple into the voter else display the “Not an
eligible voter msg”.
DELIMITER //
CREATE PROCEDURE INSERT_VOTER1(IN vid int,IN vname
varchar(20),IN vage int,IN vaddrvarchar(20),IN cons_id
int,cid int)
BEGIN
DECLARE msg varchar(70);
IF vage>=18
THEN
INSERT INTO VOTER VALUES(vid,vname,vage,vaddr,cons_id,cid);
set msg="row inserted successfully";
ELSE
set msg="voter age is less than 18";
END IF
;
SELECT msg;
END;
//
CALL insert_voter1(332,'ramesh',20,'nippani',111,121);
CALL insert_voter1(338,'ram',17,'nippani',111,121);
//
VIEWS
A view in SQL is a virtual table that provides a way to simplify complex queries, encapsulate
logic, and re-use SQL code. It doesn't store data itself but displays data from one or more tables or
other views. Views are commonly used to simplify complex queries, aggregate data, or hide
specific data from the end user.
Virtual Table: A view behaves like a table but doesn't physically store data. Instead, it
stores a query that retrieves data when accessed.
Simplifying Queries: Views can simplify complex queries, so users don't need to write the
same SQL repeatedly.
Security: Views can be used to restrict access to specific columns or rows of a table,
providing an abstraction layer.
Creating a View
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
1 Alice HR 50000
2 Bob IT 60000
4 David IT 65000
5 Eve HR 55000
To create a view that only shows the names and salaries of employees:
You can create views that include filters. For example, let's create a view for employees with a
salary greater than 55,000.
Now, querying the HighEarners view will only return employees with a salary above 55,000:
If you no longer need a view, you can remove it using the DROP VIEW statement:
Limitations of Views
1. Read-Only Views: Some views are read-only if they involve certain operations (such as
joins or aggregations). You cannot update or insert data into these views directly.
2. Performance: Since views are virtual and executed at runtime, they may impact
performance when dealing with complex queries.
3. No Indexing: Views do not have indexes, although indexes on underlying tables can still
affect performance.
4. Not Persistent Data: Views do not store data themselves; they are always generated on the
fly when queried.
A nested subquery (or simply subquery) is a query within another query. The subquery is
typically used to return a value that is then used by the outer query to filter results or perform other
operations. Subqueries can be nested in various places, such as in the SELECT, WHERE, FROM,
and HAVING clauses of an SQL query.
Important Rule:
A subquery can be placed in a number of SQL clauses like WHERE clause, FROM clause,
HAVING clause.
You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
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.
Subqueries are on the right side of the comparison operator.
A subquery is enclosed in parentheses.
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
Consider the EMPLOYEE table have the following records:
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
This would produce the following result:
4 Alina 29 UK 6500.00
SQL subquery can also be used with the Insert statement. In the insert statement, data returned
from the subquery is used to insert into another table.
In the subquery, the selected data can be modified with any of the character, date functions.
Syntax:
Example
Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.
Now use the following syntax to copy the complete EMPLOYEE table into the EMPLOYEE_BKP
table.
The subquery of SQL can be used in conjunction with the Update statement. When a subquery is used
with the Update statement, then either single or multiple columns in a table can be updated.
Syntax
UPDATE table
SET column_name = new_value
WHERE VALUE OPERATOR
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);
Example
UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement just like any other
statements mentioned above.
Syntax
1 John 20 US 2000.00