Khalid.
Khalid.
Relational model makes the query much easier than in hierarchical or network
database systems. In 1970, E.F Codd has been developed it. A relational
database is defined as a group of independent tables which are linked to each
other using some common fields of each related table. This model can be
represented as a table with columns and rows. Each row is known as a tuple.
Each table of the column has a name or attribute. It is well knows in database
technology because it is usually used to represent real-world objects and the
relationships between them. Some popular relational databases are used
nowadays like Oracle, Sybase, DB2, MySQL Server etc.
Relation Table
Let's explain each term one by one in detail with the help of example:
1
Stu_No S_Name PHONE_NO ADDRESS Gender
Tuple: The rows of a relation that contain the values corresponding to the
attributes are called tuples. For example: in the Student relation there are 5
tuples.
Data Item: The smallest unit of data in the relation is the individual data item.
It is stored at the intersection of rows and columns are also known as cells. For
Example: 10112, "Rama" etc are data items in Student relation.
Domain: It contains a set of atomic values that an attribute can take. It could be
accomplish explicitly by listing all possible values or specifying conditions that
all values in that domain must be confirmed. For example: the domain of
gender attributes is a set of data values "M" for male and "F" for female. No
2
database software fully supports domains typically allowing the users to define
very simple data types such as numbers, dates, characters etc.
Relational schema: A relational schema contains the name of the relation and
name of all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It
can identify the row in the relation uniquely.
Properties of Relations
3
o Name of the relation is distinct from all other relations.
o This model is very easy to design. Tables can use different attributes as
per requirements.
o Relational database helps the user to use a query language to query the
database.
4
o Relational database helps the user to use a query language to query the
database.
o It is useful for representing most real world objects and the relationships
between them. It is very easy to implement a relationship through the
use of a composite key, so this model persistence method dominates the
market.
o The relational model can easily adapt to new hardware so incurs large
hardware overhead.
o The relational model is suitable for small databases but not suitable for
complex databases because the user needs to know the complex
physical data storage details. So, while designing the databases they
don't come to light when they may cause problems. When a database
5
grows it will slow down the system and will result in performance
degradation and data corruption.
o Insertion Operation
o Deletion Operation
o Update Operation
o Retrieval Operation
Insert operation: It is used to insert a new record in the table. Adding new
records to the table is much easier than other models. Data values will not be
found in a relation when the following condition occurs:
o If we try to enter a data value in the foreign key attribute that does not
exist in corresponding primary key attribute.
DELETE operation: This operation is used to delete records from the table but
problems arise when the rows to be deleted have some attributes which are
foreign key attributes.
6
o The modified value does not contain the value of the corresponding
foreign key value.
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step
process to obtain the result of the query. It uses operators to perform queries.
1. Select Operation:
1. Notation: σ p(r)
Where:
7
p is used as a propositional logic formula which may use connectors like: AND
OR and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
8
o This operation shows the list of those attributes that we wish to appear
in the result. Rest of the attributes are eliminated from the table.
o It is denoted by ∏.
Where
Input:
Output:
NAME CITY
Jones Harrison
9
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all
the tuples that are either in R or S or both in R & S.
1. Notation: R ∪ S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
10
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
Output:
11
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
12
1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
1. Notation: R - S
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
13
6. Cartesian product
o The Cartesian product is used to combine each row in one table with
each row in the other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
14
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
1. ρ(STUDENT1, STUDENT)
Join Operations:
A Join operation combines related tuples from different relations, if and only if
a given join condition is satisfied. It is denoted by ⋈.
15
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Result:
16
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are
equal on their common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
Output:
EMP_NAME SALARY
17
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal
with missing information.
Example:
EMPLOYEE
FACT_WORKERS
18
Hari TCS 50000
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
o Left outer join contains the set of tuples of all combinations in R and S
that are equal on their common attribute names.
o It is denoted by ⟕.
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
19
EMP_NAME STREET CITY BRANCH SALARY
o Right outer join contains the set of tuples of all combinations in R and S
that are equal on their common attribute names.
o It is denoted by ⟖.
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
20
Shyam Wipro 20000 Park street Kolkata
o Full outer join is like a left or right join except that it contains all rows
from both tables.
o In full outer join, tuples in R that have no matching tuples in S and tuples
in S that have no matching tuples in R in their common attribute name.
o It is denoted by ⟗.
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
3. Equi join:
21
It is also known as an inner join. It is the most common join. It is based on
matched data as per the equality condition. The equi join uses the comparison
operator(=).
Example:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
1. CUSTOMER ⋈ PRODUCT
Output:
1 John 1 Delhi
22
2 Harry 2 Mumbai
3 Harry 3 Noida
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality
of information.
o Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not
affected.
1. Domain constraints
23
o Domain constraints can be defined as the definition of a valid set of
values for an attribute.
o The data type of domain includes string, character, integer, time, date,
currency, etc. The value of the attribute must be available in the
corresponding domain.
Example:
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows
in relation and if the primary key has a null value, then we can't identify
those rows.
o A table can contain a null value other than the primary key field.
Example:
24
3. Referential Integrity Constraints
Example:
25
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity
set uniquely.
o An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.
Example:
Relational Calculus
There is an alternate way of formulating queries known as Relational Calculus.
Relational calculus is a non-procedural query language. In the non-procedural
query language, the user is concerned with the details of how to obtain the
end results. The relational calculus tells what to do but never explains how to
do. Most commercial relational languages are based on aspects of relational
calculus including SQL-QBE and QUEL.
Many of the calculus expressions involves the use of Quantifiers. There are
two types of quantifiers:
26
o Universal Quantifiers: The universal quantifier denoted by ∀ is read as
for all which means that in a given set of tuples exactly all tuples satisfy a
given condition.
Free and bound variables may be compared with global and local variable of
programming languages.
27
Notation:
Where
For example:
Output: This query selects the tuples from the AUTHOR relation. It returns a
tuple with 'name' from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential
(∃) and Universal Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
Notation:
Where
28
a1, a2 are attributes
P stands for formula built by inner attributes
For example:
Output: This query will yield the article, page, and subject from the relational
javatpoint, where the subject is a database.
What is SQL?
SQL is a short-form of the structured query language, and it is pronounced as S-
Q-L or sometimes as See-Quell.
This database language is mainly designed for maintaining the data in relational
database management systems. It is a special tool used by data professionals
for handling structured data (data which is stored in the form of tables). It is
also designed for stream processing in RDSMS.
You can easily create and manipulate the database, access and modify the
table rows and columns, etc. This query language became the standard of ANSI
in the year of 1986 and ISO in the year of 1987.
If you want to get a job in the field of data science, then it is the most
important query language to learn. Big enterprises like Facebook, Instagram,
and LinkedIn, use SQL for storing the data in the back-end.
Why SQL?
Nowadays, SQL is widely used in data science and analytics. Following are the
reasons which explain why it is widely used:
o The basic use of SQL for data professionals and SQL users is to insert,
update, and delete the data from the relational database.
o SQL allows the data professionals and users to retrieve the data from the
relational database management systems.
29
o It allows SQL users to create, drop, and manipulate the database and its
tables.
o It also helps in creating the view, stored procedure, and functions in the
relational database.
o It allows you to define the data and modify that stored data in the
relational database.
History of SQL
"A Relational Model of Data for Large Shared Data Banks" was a paper which
was published by the great computer scientist "E.F. Codd" in 1970.
At the end of the 1970s, relational software Inc. developed their own first SQL
using the concepts of E.F. Codd, Raymond Boyce, and Donald Chamberlin. This
SQL was totally based on RDBMS. Relational Software Inc., which is now known
as Oracle Corporation, introduced the Oracle V2 in June 1979, which is the first
implementation of SQL language. This Oracle V2 version operates on VAX
computers.
Process of SQL
o Query Dispatcher
30
o Optimization Engines
A classic query engine allows data professionals and users to maintain non-SQL
queries. The architecture of SQL is shown in the following diagram:
The SQL commands help in creating and managing the database. The most
common SQL commands which are highly used are mentioned below:
1. CREATE command
2. UPDATE command
3. DELETE command
4. SELECT command
5. DROP command
6. INSERT command
31
CREATE Command
This command helps in creating the new database, new table, table view, and
other objects of the database.
UPDATE Command
This command helps in updating or changing the stored data in the database.
DELETE Command
This command helps in removing or erasing the saved records from the
database tables. It erases single or multiple tuples from the tables of the
database.
SELECT Command
This command helps in accessing the single or multiple rows from one or
multiple tables of the database. We can also use this command with the
WHERE clause.
DROP Command
This command helps in deleting the entire table, table view, and other objects
from the database.
INSERT Command
This command helps in inserting the data or records into the database tables.
We can easily insert the records in single as well as multiple rows of the table.
SQL vs No-SQL
32
The following table describes the differences between the SQL and NoSQL,
which are necessary to understand:
SQL No-SQL
2. The query language used in this 2. The query language used in the No-SQL
database system is a structured query database systems is a non-declarative
language. query language.
5. The database type of SQL is in the form 5. The database type of No-SQL is in the
of tables, i.e., in the form of rows and form of documents, key-value, and
columns. graphs.
8. This database is not the best choice for 8. While No-SQL database is a perfect
storing hierarchical data. option for storing hierarchical data.
9. All SQL databases require object- 9. Many No-SQL databases do not require
relational mapping. object-relational mapping.
33
10. Gauges, CircleCI, Hootsuite, etc., are 10. Airbnb, Uber, and Kickstarter are the
the top enterprises that are using this top enterprises that are using this query
query language. language.
11. SQLite, Ms-SQL, Oracle, PostgreSQL, 11. Redis, MongoDB, Hbase, BigTable,
and MySQL are examples of SQL database CouchDB, and Cassandra are examples of
systems. NoSQL database systems.
Advantages of SQL
SQL provides various advantages which make it more popular in the field of
data science. It is a perfect query language which allows data professionals and
users to communicate with the database. Following are the best advantages or
benefits of Structured Query Language:
1. No programming needed
SQL does not require a large number of coding lines for managing the database
systems. We can easily access and maintain the database by using simple SQL
syntactical rules. These simple rules make the SQL user-friendly.
A large amount of data is accessed quickly and efficiently from the database by
using SQL queries. Insertion, deletion, and updation operations on data are
also performed in less time.
3. Standardized Language
SQL follows the long-established standards of ISO and ANSI, which offer a
uniform platform across the globe to all its users.
4. Portability
5. Interactive language
34
We can easily learn and understand the SQL language. We can also use this
language for communicating with the database because it is a simple query
language. This language is also used for receiving the answers to complex
queries in a few seconds.
The SQL language also helps in making the multiple views of the database
structure for the different database users.
Disadvantages of SQL
With the advantages of SQL, it also has some disadvantages, which are as
follows:
1. Cost
The operation cost of some SQL versions is high. That's why some programmers
cannot use the Structured Query Language.
2. Interface is Complex
The business rules are hidden. So, the data professionals and users who are
using this query language cannot have full database control.
Data types are used to represent the nature of the data that can be stored in
the database table. For example, in a particular column of a table, if we want to
store a string type of data then we will have to declare a string data type of this
column.
Data types mainly classified into three categories for every database.
35
o Date and time Data types
A list of data types used in MySQL database. This is based on MySQL 8.0.
36
It holds a string with a maximum length of
MEDIUMTEXT
16,777,215.
37
INTEGER(size) It is equal to INT(size).
38
It is used to specify Boolean values true
BOOL and false. Zero is considered as false, and
nonzero values are considered as true.
39
It is a fixed width character string dat
char(n)
can be up to 8000 characters.
40
tinyint It allows whole numbers from 0 to 255.
41
It is used to specify date and time
combination. It supports range from
datetime2
January 1, 0001 to December 31, 9999
with an accuracy of 100 nanoseconds
42
uniqueidentifier It stores GUID (Globally unique identifier).
43
It is a subtype of the NUMBER data type.
FLOAT(p)
The precision p can range from 1 to 126.
44
It is used to specify single byte or fixed
length multibyte national character set
NCLOB
(NCHAR) data. Its range is up to 232-1
bytes or 4 GB.
SQL Operators
Every database administrator and user uses SQL queries for manipulating and
accessing the data of database tables and views.
The manipulation and retrieving of the data are performed with the help of
reserved words and characters, which are used to perform arithmetic
operations, logical operations, comparison operations, compound operations,
etc.
The SQL reserved words and characters are called operators, which are used
with a WHERE clause in a SQL query. In SQL, an operator can either be a unary
or binary operator. The unary operator uses only one operand for performing
the unary operation, whereas the binary operator uses two operands for
performing the binary operation.
1. Operator SQL_Operand
Note: SQL operators are used for filtering the table's data by a specific
condition in the SQL statement.
The precedence of SQL operators is the sequence in which the SQL evaluates
the different operators in the same expression. Structured Query Language
evaluates those operators first, which have high precedence.
In the following table, the operators at the top have high precedence, and the
operators that appear at the bottom have low precedence.
** Exponentiation operator
OR Inclusion operator
For Example,
46
1. UPDATE employee
In the above SQL example, salary is assigned 5, not 85, because the *
(Multiplication)
Types of Operator
Following are the various arithmetic operators performed on the SQL data:
47
SQL Addition Operator (+)
The Addition Operator in SQL performs the addition on the numerical data of
the database table. In SQL, we can easily add the numerical values of two
columns of the same table by specifying both the column names as the first
and second operand. We can also add the numbers to the existing numbers of
the specific column.
Let's understand the below example which explains how to execute Addition
Operator in SQL query:
In this query, we have performed the SQL addition operation on the single
column of the given table.
o Suppose, we want to add the Salary and monthly bonus columns of the
above table, then we have to write the following query in SQL:
48
1. SELECT Emp_Salary + Emp_Monthlybonus as Emp_Total_Salary FROM E
mployee_details;
In this query, we have added two columns with each other of the above table.
1. SELECT Emp_Salary -
5000 as Emp_New_Salary FROM Employee_details;
In this query, we have performed the SQL subtraction operation on the single
column of the given table.
49
o If we want to subtract the penalty from the salary of each employee,
then we have to write the following query in SQL:
1. SELECT Emp_Salary -
Penalty as Emp_Total_Salary FROM Employee_details;
In this query, we have performed the SQL multiplication operation on the single
column of the given table.
50
o If we want to multiply the Emp_Id column to Emp_Salary column of that
employee whose Emp_Id is 202, then we have to write the following
query in SQL:
In this query, we have multiplied the values of two columns by using the
WHERE clause.
The Division Operator in SQL divides the operand on the left side by the
operand on the right side.
In SQL, we can also divide the numerical values of one column by another
column of the same table by specifying both column names as the first and
second operand.
We can also perform the division operation on the stored numbers in the
column of the SQL table.
Let's understand the below example which explains how to execute Division
Operator in SQL query:
51
o Suppose, we want to half the salary of each employee given in the
Employee_details table. For this operation, we have to write the
following query in the SQL:
In this query, we have performed the SQL division operation on the single
column of the given table.
The Modulus Operator in SQL provides the remainder when the operand on
the left side is divided by the operand on the right side.
Let's understand the below example which explains how to execute Modulus
Operator in SQL query:
This example consists of a Division table, which has three columns Number,
First_operand, and Second_operand.
1 56 4
2 32 8
3 89 9
4 18 10
5 10 5
52
o If we want to get the remainder by dividing the numbers of
First_operand column by the numbers of Second_operand column, then
we have to write the following query in SQL:
The Comparison Operators in SQL compare two different data of SQL table and
check whether they are the same, greater, and lesser. The SQL comparison
operators are used with the WHERE clause in the SQL queries
Following are the various comparison operators which are performed on the
data stored in the SQL database tables:
This operator is highly used in SQL queries. The Equal Operator in SQL shows
only data that matches the specified value in the query.
This operator returns TRUE records from the database table if the value of both
operands specified in the query is matched.
Let's understand the below example which explains how to execute Equal
Operator in SQL query:
53
Emp Id Emp Name Emp Salary
In this example, we used the SQL equal operator with WHERE clause for getting
the records of those employees whose salary is 30000.
The Equal Not Operator in SQL shows only those data that do not match the
query's specified value.
This operator returns those records or rows from the database views and
tables if the value of both operands specified in the query is not matched with
each other.
Let's understand the below example which explains how to execute Equal
Not Operator in SQL query:
54
201 Abhay 45000
In this example, we used the SQL equal not operator with WHERE clause for
getting the records of those employees whose salary is not 45000.
The Greater Than Operator in SQL shows only those data which are greater
than the value of the right-hand operand.
Let's understand the below example which explains how to execute Greater
ThanOperator (>) in SQL query:
55
203 Bheem 30000
Here, SQL greater than operator displays the records of those employees from
the above table whose Employee Id is greater than 202.
The Greater Than Equals to Operator in SQL shows those data from the table
which are greater than and equal to the value of the right-hand operand.
Let's understand the below example which explains how to execute greater
than equals to the operator (>=) in SQL query:
56
205 Sumit 29000
Here,'SQL greater than equals to operator' with WHERE clause displays the
rows of those employees from the table whose Employee Id is greater than and
equals to 202.
The Less Than Operator in SQL shows only those data from the database tables
which are less than the value of the right-side operand.
This comparison operator checks that the left side operand is lesser than the
right side operand. If the condition becomes true, then this operator in SQL
displays the data which is less than the value of the right-side operand.
Let's understand the below example which explains how to execute less than
operator (<) in SQL query:
57
205 Sumit 29000
Here,SQL less than operator with WHERE clause displays the records of those
employees from the above table whose Employee Id is less than 204.
The Less Than Equals to Operator in SQL shows those data from the table
which are lesser and equal to the value of the right-side operand.
This comparison operator checks that the left side operand is lesser and equal
to the right side operand.
Let's understand the below example which explains how to execute less than
equals to the operator (<=) in SQL query:
58
o Suppose, we want to access all the records of those employees from
the Employee_details table whose employee id is less and
equals 203. For this, we have to write the following query in the SQL
database:
Here, SQL less than equals to the operator with WHERE clause displays the
rows of those employees from the table whose Employee Id is less than and
equals 202.
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.
Following are the various logical operators which are performed on the data
stored in the SQL database tables:
3. SQL OR operator
5. SQL IN operator
The ALL operator in SQL compares the specified value to all the values of a
column from the sub-query in the SQL database.
59
1. SELECT,
2. HAVING, and
3. WHERE.
Let's understand the below example which explains how to execute ALL
logical operators in SQL query:
60
Here, we used the SQL ALL operator with greater than the operator.
The AND operator in SQL would show the record from the database table if all
the conditions separated by the AND operator evaluated to True. It is also
known as the conjunctive operator and is used with the WHERE clause.
Let's understand the below example which explains how to execute AND
logical operator in SQL query:
61
Here,SQL AND operator with WHERE clause shows the record of employees
whose salary is 25000 and the city is Delhi.
SQL OR Operator
The OR operator in SQL shows the record from the table if any of the
conditions separated by the OR operator evaluates to True. It is also known as
the conjunctive operator and is used with the WHERE clause.
Syntax of OR operator:
62
Here, SQL OR operator with WHERE clause shows the record of employees
whose salary is 25000 or the city is Delhi.
The BETWEEN operator in SQL shows the record within the range mentioned in
the SQL query. This operator operates on the numbers, characters, and
date/time operands.
If there is no value in the given range, then this operator shows NULL value.
63
1. SELECT * FROM Employee_details WHERE Emp_Salary BETWEEN 30000
AND 45000;
Here, we used the SQL BETWEEN operator with the Emp_Salary field.
SQL IN Operator
The IN operator in SQL allows database users to specify two or more values in a
WHERE clause. This logical operator minimizes the requirement of multiple OR
conditions.
This operator makes the query easier to learn and understand. This operator
returns those rows whose values match with any value of the given list.
Syntax of IN operator:
Let's understand the below example which explains how to execute IN logical
operator in SQL query:
64
o Suppose, we want to show all the information of those employees from
the Employee_details table whose Employee Id is 202, 204, and 205. For
this, we have to write the following query in SQL:
2.
Here, we used the SQL NOT IN operator with the Emp_Id column.
The NOT operator in SQL shows the record from the table if the condition
evaluates to false. It is always used with the WHERE clause.
Let's understand the below example which explains how to execute NOT
logical operator in SQL query:
65
203 Bheem 30000 Delhi
In this example, we used the SQL NOT operator with the Emp_City column.
In this example, we used the SQL NOT operator with the Emp_City column.
The ANY operator in SQL shows the records when any of the values returned
by the sub-query meet the condition.
The ANY logical operator must match at least one record in the inner query and
must be preceded by any SQL comparison operator.
The LIKE operator in SQL shows those records from the table which match with
the given pattern specified in the sub-query.
66
The percentage (%) sign is a wildcard which is used in conjunction with this
logical operator.
This operator is used in the WHERE clause with the following three statements:
1. SELECT statement
2. UPDATE statement
3. DELETE statement
Let's understand the below example which explains how to execute LIKE
logical operator in SQL query:
67
In this example, we used the SQL LIKE operator with Emp_Name column
because we want to access the record of those employees whose name starts
with s.
The Set Operators in SQL combine a similar type of data from two or more SQL
database tables. It mixes the result, which is extracted from two or more SQL
queries, into a single result.
Set operators combine more than one select statement in a single query and
return a specific result set.
Following are the various set operators which are performed on the similar
data stored in the two SQL database tables:
The SQL Union Operator combines the result of two or more SELECT
statements and provides the single output.
68
The data type and the number of columns must be the same for each SELECT
statement used with the UNION operator. This operator does not show the
duplicate records in the output table.
2. UNION
Let's understand the below example which explains how to execute Union
operator in Structured Query Language:
In this example, we used two tables. Both tables have four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Table: Employee_details1
69
205 Ram 30000 Kerala
Table: Employee_details2
2. UNION
The SQL Union Operator is the same as the UNION operator, but the only
difference is that it also shows the same record.
2. UNION ALL
Let's understand the below example which explains how to execute Union
ALL operator in Structured Query Language:
In this example, we used two tables. Both tables have four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
70
201 Sanjay 25000 Delhi
Table: Employee_details1
Table: Employee_details2
2. UNION ALL
71
The SQL Intersect Operator shows the common record from two or more
SELECT statements. The data type and the number of columns must be the
same for each SELECT statement used with the INTERSECT operator.
2. INTERSECT
In this example, we used two tables. Both tables have four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Table: Employee_details1
72
203 Saket 30000 Aligarh
Table: Employee_details2
Suppose, we want to see a common record of the employee from both the
tables in a single output. For this, we have to write the following query in SQL:
2. INTERSECT
The SQL Minus Operator combines the result of two or more SELECT
statements and shows only the results from the first data set.
2. MINUS
In this example, we used two tables. Both tables have four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
73
Emp Id Emp Name Emp Salary Emp City
Table: Employee_details1
Table: Employee_details2
Suppose, we want to see the name of employees from the first result set after
the combination of both tables. For this, we have to write the following query
in SQL:
2. MINUS
The Unary Operators in SQL perform the unary operations on the single data of
the SQL table, i.e., these operators operate only on one operand.
74
These types of operators can be easily operated on the numeric data value of
the SQL table.
Following are the various unary operators which are performed on the
numeric data stored in the SQL table:
The SQL Positive (+) operator makes the numeric value of the SQL table
positive.
Let's understand the below example which explains how to execute a Positive
unary operator on the data of SQL table:
75
205 Sumit 40000 Kolkata
The SQL Negative (-) operator makes the numeric value of the SQL table
negative.
Let's understand the below example which explains how to execute Negative
unary operator on the data of SQL table:
76
o Suppose, we want to see the salary of each employee as negative from
the Employee_details table. For this, we have to write the following
query in SQL:
The SQL Bitwise NOT operator provides the one's complement of the single
numeric operand. This operator turns each bit of numeric value. If the bit of
any numerical value is 001100, then this operator turns these bits into 110011.
Let's understand the below example which explains how to execute the
Bitwise NOT operator on the data of SQL table:
77
105 Sumit 60 Kolkata
The Bitwise Operators in SQL perform the bit operations on the Integer values.
To understand the performance of Bitwise operators, you just knew the basics
of Boolean algebra.
Following are the two important logical operators which are performed on
the data stored in the SQL database tables:
2. Bitwise OR(|)
The Bitwise AND operator performs the logical AND operation on the given
Integer values. This operator checks each bit of a value with the corresponding
bit of another value.
1. SELECT column1 & column2 & .... & columnN FROM table_Name [WHER
E conditions] ;
Let's understand the below example which explains how to execute Bitwise
AND operator on the data of SQL table:
This example consists of the following table, which has two columns. Each
column holds numerical values.
When we use the Bitwise AND operator in SQL, then SQL converts the values of
both columns in binary format, and the AND operation is performed on the
converted bits.
78
After that, SQL converts the resultant bits into user understandable format, i.e.,
decimal format.
Column1 Column2
1 1
2 5
3 4
4 2
5 3
Bitwise OR (|)
The Bitwise OR operator performs the logical OR operation on the given Integer
values. This operator checks each bit of a value with the corresponding bit of
another value.
Let's understand the below example which explains how to execute Bitwise
OR operator on the data of SQL table:
This example consists of a table that has two columns. Each column holds
numerical values.
79
When we used the Bitwise OR operator in SQL, then SQL converts the values of
both columns in binary format, and the OR operation is performed on the
binary bits. After that, SQL converts the resultant binary bits into user
understandable format, i.e., decimal format.
Column1 Column2
1 1
2 5
3 4
4 2
5 3
Characteristics of SQL
o SQL is used to define the data in the database and manipulate it when
needed.
80
o SQL is used to create a view, stored procedure, function in a database.
Advantages of SQL
There are the following advantages of SQL:
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of records
from a database.
No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't require a
substantial amount of code to manage the database system.
Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
Interactive language
SQL is a domain language used to communicate with the database. It is also used to receive
answers to the complex questions in seconds.
81