0% found this document useful (0 votes)
17 views56 pages

UNIT2

Uploaded by

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

UNIT2

Uploaded by

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

Structured Query Language (SQL): Introduction, features,

characteristics, advantages, data types, operators, Components of SQL:


DDL, DML, DCL and TCL commands, Select statement with
Clauses( Order By, Group By, Having, Where)

What is SQL?
 SQL stands for Structured Query Language

 SQL lets you access and manipulate databases

 SQL became a standard of the American National Standards Institute


(ANSI) in 1986, and of the International Organization for Standardization
(ISO) in 1987

What Can SQL do?


 SQL can execute queries against a database

 SQL can retrieve data from a database

 SQL can insert records in a database

 SQL can update records in a database

 SQL can delete records from a database

 SQL can create new databases

 SQL can create new tables in a database

 SQL can create stored procedures in a database

 SQL can create views in a database

 SQL can set permissions on tables, procedures, and views


Features Of SQL
The following features of Structured Query Language make it a solid primary but robust
language for lots of organizations looking to manage their large databases:
#1. Flexibility and Scalability
SQL offers users flexibility and scalability for relational database management systems. With
SQL, it is easier to create new tables while dropping or deleting previously-created or scantily
used tables.
#2. A Comprehensive Application Development Tool
Programmers use SQL to program applications to access a database, making it a comprehensive
and effective application development tool. SQL is suitable for every large or small organization,
no matter the size.
#3. Rich Transactional Support
It has rich transactional support. Structured Query Language’s programming capability of
handling large records while also managing several other transactions is top-notch.
#4. High Performance
Another feature of SQL is that it offers a high-performance programming capability for high
usage, incredibly transactional, and heavy workload database systems. Structured Query
Language’s programming provides different ways to describe data more analytically.
#5. High Availability
SQL is compatible with other databases such as Microsoft SQL Server, Oracle Database, MS
Access, MySQL, SAP Adaptive Server, and more.
These RDBMSs support SQL, and it is easier to create application extensions for procedural
programming and several other SQL functions that are extra features, hence making SQL a
strong tool.
#6. High Security
SQL also has high security as one of its notable features. It’s easy to give permissions on views,
procedures, and tables. So with SQL, you get optimum security for your data.
#7. SQL’s Management Ease
Almost every Relational Database Management System uses Structured Query Language. Some
of the common and standard SQL commands include “Delete,” “Insert,” “Select,” “Update,” and
“Drop.” These commands help users manage large amounts of data from a database efficiently
and quickly.
#8. Open Source
Structured Query Language has the feature of being an open-source programming language great
for building relational database management systems (RDBMS). This makes it a great pick for
developers and programmers who are looking for a community of professionals to learn from
off, and it’s also one of the benefits of SQL.
Characteristics of SQL
o SQL is easy to learn.

o SQL is used to access data from relational database management systems.

o SQL can execute queries against the database.

o SQL is used to describe the data.

o SQL is used to define the data in the database and manipulate it when needed.

o SQL is used to create and drop the database and table.

o SQL is used to create a view, stored procedure, function in a database.

o SQL allows users to set permissions on tables, procedures, and views.

The following table describes the differences between the SQL and NoSQL, which are
necessary to understand:
SQL No-SQL

1. SQL is a relational database management 1. While No-SQL is a non-relational or distributed


system. database management system.

2. The query language used in this database 2. The query language used in the No-SQL
system is a structured query language. database systems is a non-declarative query
language.

3. The schema of SQL databases is 3. The schema of No-SQL databases is a dynamic


predefined, fixed, and static. schema for unstructured data.

4. These databases are vertically scalable. 4. These databases are horizontally scalable.

5. The database type of SQL is in the form of 5. The database type of No-SQL is in the form of
tables, i.e., in the form of rows and columns. documents, key-value, and graphs.

6. It follows the ACID model. 6. It follows the BASE model.

7. Complex queries are easily managed in the 7. NoSQL databases cannot handle complex
SQL database. queries.

8. This database is not the best choice for 8. While No-SQL database is a perfect option for
storing hierarchical data. storing hierarchical data.

9. All SQL databases require object-relational 9. Many No-SQL databases do not require object-
mapping. relational mapping.

10. Gauges, CircleCI, Hootsuite, etc., are the 10. Airbnb, Uber, and Kickstarter are the top
top enterprises that are using this query enterprises that are using this query language.
language.

11. SQLite, Ms-SQL, Oracle, PostgreSQL, and 11. Redis, MongoDB, Hbase, BigTable, CouchDB,
MySQL are examples of SQL database and Cassandra are examples of NoSQL database
systems. 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.

2. High-Speed Query Processing

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

The structured query language can be easily used in desktop computers, laptops,
tablets, and even smartphones. It can also be used with other applications according to
the user's requirements.

5. Interactive language

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.

6. More than one Data View

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
Another big disadvantage is that the interface of Structured query language is difficult,
which makes it difficult for SQL users to use and manage it.

3. Partial Database control

The business rules are hidden. So, the data professionals and users who are using this
query language cannot have full database control.

SQL Data Types


Each column in a database table is required to have a name and a data type.

An SQL developer must decide what type of data that will be stored inside each
column when creating a table. The data type is a guideline for SQL to
understand what type of data is expected inside of each column, and it also
identifies how SQL will interact with the stored data.

String Data Types


Data type Description

CHAR(size) A FIXED length string (can contain letters, numbers, and special characters).
The size parameter specifies the column length in characters - can be from 0 to
255. Default is 1

VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters).
The size parameter specifies the maximum string length in characters - can be
from 0 to 65535

BINARY(size) Equal to CHAR(), but stores binary byte strings. The size parameter specifies the
column length in bytes. Default is 1

VARBINARY(size) Equal to VARCHAR(), but stores binary byte strings. The size parameter specifies
the maximum column length in bytes.

TINYBLOB For BLOBs (Binary Large Objects). Max length: 255 bytes

TINYTEXT Holds a string with a maximum length of 255 characters

TEXT(size) Holds a string with a maximum length of 65,535 bytes

BLOB(size) For BLOBs (Binary Large Objects). Holds up to 65,535 bytes of data

MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters

MEDIUMBLOB For BLOBs (Binary Large Objects). Holds up to 16,777,215 bytes of data

LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters


Numeric Data Types
Data type Description

BIT(size) A bit-value type. The number of bits per value is specified in size. The size parameter
can hold a value from 1 to 64. The default value for size is 1.

TINYINT(size) A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255.
The size parameter specifies the maximum display width (which is 255)

BOOL Zero is considered as false, nonzero values are considered as true.

BOOLEAN Equal to BOOL

SMALLINT(size) A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to
65535. The size parameter specifies the maximum display width (which is 255)

MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from 0
to 16777215. The size parameter specifies the maximum display width (which is 255)

INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is
from 0 to 4294967295. The size parameter specifies the maximum display width (which
is 255)

INTEGER(size) Equal to INT(size)

BIGINT(size) A large integer. Signed range is from -9223372036854775808 to


9223372036854775807. Unsigned range is from 0 to 18446744073709551615.
The size parameter specifies the maximum display width (which is 255)

FLOAT(size, d) A floating point number. The total number of digits is specified in size. The number of
digits after the decimal point is specified in the d parameter. This syntax is deprecated
in MySQL 8.0.17, and it will be removed in future MySQL versions
Note: All the numeric data types may have an extra option: UNSIGNED or
ZEROFILL. If you add the UNSIGNED option, MySQL disallows negative values
for the column. If you add the ZEROFILL option, MySQL automatically also adds
the UNSIGNED attribute to the column.

Date and Time Data Types


Data type Description

DATE A date. Format: YYYY-MM-DD. The supported range is from '1000-


01-01' to '9999-12-31'

DATETIME(fsp) A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The


supported range is from '1000-01-01 00:00:00' to '9999-12-31
23:59:59'. Adding DEFAULT and ON UPDATE in the column
definition to get automatic initialization and updating to the current
date and time

TIMESTAMP(fsp) A timestamp. TIMESTAMP values are stored as the number of


seconds since the Unix epoch ('1970-01-01 00:00:00' UTC).
Format: YYYY-MM-DD hh:mm:ss. The supported range is from
'1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC.
Automatic initialization and updating to the current date and time
can be specified using DEFAULT CURRENT_TIMESTAMP and ON
UPDATE CURRENT_TIMESTAMP in the column definition

TIME(fsp) A time. Format: hh:mm:ss. The supported range is from '-


838:59:59' to '838:59:59'

YEAR A year in four-digit format. Values allowed in four-digit format:


1901 to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.

What is SQL Operator?


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.

Types of Operator
SQL operators are categorized in the following categories:

1. SQL Arithmetic Operators

2. SQL Comparison Operators

3. SQL Logical Operators

4. SQL Set Operators

5. SQL Bit-wise Operators

6. SQL Unary Operators

SQL Arithmetic Operators


The Arithmetic Operators perform the mathematical operation on the numerical data
of the SQL tables. These operators perform addition, subtraction, multiplication, and
division operations on the numerical operands.

Following are the various arithmetic operators performed on the SQL data:

1. SQL Addition Operator (+)

2. SQL Subtraction Operator (-)

3. SQL Multiplication Operator (+)

4. SQL Division Operator (-)

5. SQL Modulus Operator (+)

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.

Syntax of SQL Addition Operator:


1. SELECT operand1 + operand2;
Let's understand the below example which explains how to execute Addition
Operator in SQL query:

This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.

Emp Id Emp Name Emp Salary Emp Monthlybonus

101 Tushar 25000 4000

102 Anuj 30000 200

o Suppose, we want to add 20,000 to the salary of each employee specified in the table.
Then, we have to write the following query in the SQL:

1. SELECT Emp_Salary + 20000 as Emp_New_Salary FROM Employee_details;


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:

1. SELECT Emp_Salary + Emp_Monthlybonus as Emp_Total_Salary FROM Employee_


details;
In this query, we have added two columns with each other of the above table.

SQL Subtraction Operator (-)


The Subtraction Operator in SQL performs the subtraction on the numerical data of the
database table. In SQL, we can easily subtract 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 subtract the number from the existing number of the specific table column.

Syntax of SQL Subtraction Operator:


1. SELECT operand1 - operand2;
Let's understand the below example which explains how to execute Subtraction
Operator in SQL query:

This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.

Emp Id Emp Name Emp Salary Penalty

201 Abhay 25000 200

202 Sumit 30000 500

o Suppose we want to subtract 5,000 from the salary of each employee given in
the Employee_details table. Then, we have to write the following query in the SQL:

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.

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;

SQL Multiplication Operator (*)


The Multiplication Operator in SQL performs the Multiplication on the numerical data of
the database table. In SQL, we can easily multiply the numerical values of two columns
of the same table by specifying both the column names as the first and second operand.

Syntax of SQL Multiplication Operator:


1. SELECT operand1 * operand2;
Let's understand the below example which explains how to execute Multiplication
Operator in SQL query:

This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
Emp Id Emp Name Emp Salary Penalty

201 Abhay 25000 200

202 Sumit 30000 500

o Suppose, we want to double the salary of each employee given in


the Employee_details table. Then, we have to write the following query in the SQL:

1. SELECT Emp_Salary * 2 as Emp_New_Salary FROM Employee_details;


In this query, we have performed the SQL multiplication operation on the single column
of the given table.

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:

1. SELECT Emp_Id * Emp_Salary as Emp_Id * Emp_Salary FROM Employee_details W


HERE Emp_Id = 202;
In this query, we have multiplied the values of two columns by using the WHERE clause.

SQL Division Operator (/)


The Division Operator in SQL divides the operand on the left side by the operand on the
right side.

Syntax of SQL Division Operator:


1. SELECT operand1 / operand2;
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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.

Emp Id Emp Name Emp Salary

201 Abhay 25000

202 Sumit 30000

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:

1. SELECT Emp_Salary / 2 as Emp_New_Salary FROM Employee_details;


In this query, we have performed the SQL division operation on the single column of the
given table.

SQL Modulus Operator (%)


The Modulus Operator in SQL provides the remainder when the operand on the left side
is divided by the operand on the right side.

Syntax of SQL Modulus Operator:


1. SELECT operand1 % operand2;
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.
Number First operand Second operand

1 56 4

2 32 8

3 89 9

4 18 10

5 10 5

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:

1. SELECT First_operand % Second_operand as Remainder FROM Employee_details;

SQL Comparison Operators


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:

1. SQL Equal Operator (=)

2. SQL Not Equal Operator (!=)

3. SQL Greater Than Operator (>)

4. SQL Greater Than Equals to Operator (>=)

5. SQL Less Than Operator (<)\

6. SQL Less Than Equals to Operator (<=)


SQL Equal Operator (=)
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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.

Emp Id Emp Name Emp Salary

201 Abhay 30000

202 Ankit 40000

203 Bheem 30000

204 Ram 29000

205 Sumit 30000

o Suppose, we want to access all the records of those employees from


the Employee_details table whose salary is 30000. Then, we have to write the following
query in the SQL database:

1. SELECT * FROM Employee_details WHERE Emp_Salary = 30000;


In this example, we used the SQL equal operator with WHERE clause for getting the
records of those employees whose salary is 30000.

SQL Equal Not Operator (!=)


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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.

Emp Id Emp Name Emp Salary

201 Abhay 45000

202 Ankit 45000

203 Bheem 30000

204 Ram 29000

205 Sumit 29000

o Suppose, we want to access all the records of those employees from


the Employee_details table whose salary is not 45000. Then, we have to write the
following query in the SQL database:

1. SELECT * FROM Employee_details WHERE Emp_Salary != 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.

SQL Greater Than Operator (>)


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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Emp Id Emp Name Emp Salary

201 Abhay 45000

202 Ankit 45000

203 Bheem 30000

204 Ram 29000

205 Sumit 29000

o Suppose, we want to access all the records of those employees from


the Employee_details table whose employee id is greater than 202. Then, we have to
write the following query in the SQL database:

1. SELECT * FROM Employee_details WHERE Emp_Id > 202;


Here, SQL greater than operator displays the records of those employees from the
above table whose Employee Id is greater than 202.

SQL Greater Than Equals to Operator (>=)


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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Emp Id Emp Name Emp Salary

201 Abhay 45000

202 Ankit 45000

203 Bheem 30000

204 Ram 29000

205 Sumit 29000

o Suppose, we want to access all the records of those employees from


the Employee_details table whose employee id is greater than and equals to 202. For
this, we have to write the following query in the SQL database:

1. SELECT * FROM Employee_details WHERE Emp_Id >= 202;


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.

SQL Less Than Operator (<)


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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Emp Id Emp Name Emp Salary

201 Abhay 45000

202 Ankit 45000

203 Bheem 30000

204 Ram 29000

205 Sumit 29000

o Suppose, we want to access all the records of those employees from


the Employee_details table whose employee id is less than 204. For this, we have to
write the following query in the SQL database:

1. SELECT * FROM Employee_details WHERE Emp_Id < 204;


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.

SQL Less Than Equals to Operator (<=)


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:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Emp Id Emp Name Emp Salary

201 Abhay 45000

202 Ankit 45000

203 Bheem 30000

204 Ram 29000

205 Sumit 29000

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:

1. SELECT * FROM Employee_details WHERE Emp_Id <= 203;


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.

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

Following are the various logical operators which are performed on the data
stored in the SQL database tables:

1. SQL ALL operator

2. SQL AND operator

3. SQL OR operator

4. SQL BETWEEN operator

5. SQL IN operator
6. SQL NOT operator

7. SQL ANY operator

8. SQL LIKE operator

SQL ALL 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.

This operator is always used with the following statement:

1. SELECT,

2. HAVING, and

3. WHERE.

Syntax of ALL operator:


1. SELECT column_Name1, ...., column_NameN FROM table_Name WHERE column C
omparison_operator ALL (SELECT column FROM tablename2)
Let's understand the below example which explains how to execute ALL logical
operators in SQL query:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.

Emp Id Emp Name Emp Salary Emp City

201 Abhay 25000 Gurgaon

202 Ankit 45000 Delhi

203 Bheem 30000 Jaipur

204 Ram 29000 Mumbai

205 Sumit 40000 Kolkata


o If we want to access the employee id and employee names of those employees from the
table whose salaries are greater than the salary of employees who lives in Jaipur city,
then we have to type the following query in SQL.

1. SELECT Emp_Id, Emp_Name FROM Employee_details WHERE Emp_Salary > ALL (


SELECT Emp_Salary FROM Employee_details WHERE Emp_City = Jaipur)
Here, we used the SQL ALL operator with greater than the operator.

SQL AND 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.

Syntax of AND operator:


1. SELECT column1, ...., columnN FROM table_Name WHERE condition1 AND conditi
on2 AND condition3 AND ....... AND conditionN;
Let's understand the below example which explains how to execute AND logical
operator in SQL query:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Emp Id Emp Name Emp Salary Emp City

201 Abhay 25000 Delhi

202 Ankit 45000 Chandigarh

203 Bheem 30000 Delhi

204 Ram 25000 Delhi

205 Sumit 40000 Kolkata

o Suppose, we want to access all the records of those employees from


the Employee_details table whose salary is 25000 and the city is Delhi. For this, we have
to write the following query in SQL:

1. SELECT * FROM Employee_details WHERE Emp_Salary = 25000 OR Emp_Cit


y = 'Delhi';
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:
1. SELECT column1, ...., columnN FROM table_Name WHERE condition1 OR conditio
n2 OR condition3 OR ....... OR conditionN;
Let's understand the below example which explains how to execute OR logical
operator in SQL query:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Emp Id Emp Name Emp Salary Emp City

201 Abhay 25000 Delhi

202 Ankit 45000 Chandigarh

203 Bheem 30000 Delhi

204 Ram 25000 Delhi

205 Sumit 40000 Kolkata

o If we want to access all the records of those employees from the Employee_details table
whose salary is 25000 or the city is Delhi. For this, we have to write the following query in
SQL:

1. SELECT * FROM Employee_details WHERE Emp_Salary = 25000 OR Emp_City = 'D


elhi';
Here, SQL OR operator with WHERE clause shows the record of employees whose
salary is 25000 or the city is Delhi.

SQL BETWEEN Operator


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.

Syntax of BETWEEN operator:


1. SELECT column_Name1, column_Name2 ...., column_NameN FROM table_Name
WHERE column_nameBETWEEN value1 and value2;
Let's understand the below example which explains how to execute BETWEEN
logical operator in SQL query:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Emp Id Emp Name Emp Salary Emp City

201 Abhay 25000 Delhi

202 Ankit 45000 Chandigarh

203 Bheem 30000 Delhi

204 Ram 25000 Delhi

205 Sumit 40000 Kolkata

o Suppose, we want to access all the information of those employees from


the Employee_details table who is having salaries between 20000 and 40000. For this,
we have to write the following query in SQL:

1. SELECT * FROM Employee_details WHERE Emp_Salary BETWEEN 30000 AND 4500


0;
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:
1. SELECT column_Name1, column_Name2 ...., column_NameN FROM table_Name
WHERE column_name IN (list_of_values);
Let's understand the below example which explains how to execute IN logical
operator in SQL query:

This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Emp Id Emp Name Emp Salary Emp City

201 Abhay 25000 Delhi

202 Ankit 45000 Chandigarh

203 Bheem 30000 Delhi

204 Ram 25000 Delhi

205 Sumit 40000 Kolkata

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:

1. SELECT * FROM Employee_details WHERE Emp_Id IN (202, 204, 205);


Here, we used the SQL IN operator with the Emp_Id column.

o Suppose, we want to show all the information of those employees from


the Employee_details table whose Employee Id is not equal to 202 and 205. For this, we
have to write the following query in SQL:

1. SELECT * FROM Employee_details WHERE Emp_Id NOT IN (202,205);


2.
Here, we used the SQL NOT IN operator with the Emp_Id column.

SQL NOT Operator


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.

Syntax of NOT operator:


1. SELECT column1, column2 ...., columnN FROM table_Name WHERE NOT conditio
n;
Let's understand the below example which explains how to execute NOT logical
operator in SQL query:

This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.

Emp Id Emp Name Emp Salary Emp City

201 Abhay 25000 Delhi

202 Ankit 45000 Chandigarh

203 Bheem 30000 Delhi

204 Ram 25000 Delhi

205 Sumit 40000 Kolkata

o Suppose, we want to show all the information of those employees from


the Employee_details table whose Cityis not Delhi. For this, we have to write the
following query in SQL:

1. SELECT * FROM Employee_details WHERE NOT Emp_City = 'Delhi' ;


In this example, we used the SQL NOT operator with the Emp_City column.

o Suppose, we want to show all the information of those employees from


the Employee_details table whose Cityis not Delhi and Chandigarh. For this, we have to
write the following query in SQL:

1. SELECT * FROM Employee_details WHERE NOT Emp_City = 'Delhi' AND NOT Emp
_City = 'Chandigarh';
In this example, we used the SQL NOT operator with the Emp_City column.

SQL ANY Operator


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.

Syntax of ANY operator:


1. SELECT column1, column2 ...., columnN FROM table_Name WHERE column_name
comparison_operator ANY ( SELECT column_name FROM table_name WHERE con
dition(s)) ;

SQL LIKE Operator


The LIKE operator in SQL shows those records from the table which match with the
given pattern specified in the sub-query.

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

Syntax of LIKE operator:


1. SELECT column_Name1, column_Name2 ...., column_NameN FROM table_Name
WHERE column_name LIKE pattern;
Let's understand the below example which explains how to execute LIKE logical
operator in SQL query:

This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Chandigarh

203 Saket 30000 Delhi

204 Abhay 25000 Delhi

205 Sumit 40000 Kolkata

o If we want to show all the information of those employees from


the Employee_details whose name starts with ''s''. For this, we have to write the
following query in SQL:

1. SELECT * FROM Employee_details WHERE Emp_Name LIKE 's%' ;


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.

o If we want to show all the information of those employees from


the Employee_detailswhose name ends with ''y''. For this, we have to write the following
query in SQL:

1. SELECT * FROM Employee_details WHERE Emp_Name LIKE '%y' ;


o If we want to show all the information of those employees from
the Employee_detailswhose name starts with ''S'' and ends with ''y''. For this, we have to
write the following query in SQL:

1. SELECT * FROM Employee_details WHERE Emp_Name LIKE 'S%y' ;

SQL Set Operators


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:

1. SQL Union Operator

2. SQL Union ALL Operator

3. SQL Intersect Operator

4. SQL Minus Operator

SQL Union Operator


The SQL Union Operator combines the result of two or more SELECT statements and
provides the single output.

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.

Syntax of UNION Set operator:


1. SELECT column1, column2 ...., columnN FROM table_Name1 [WHERE conditions]

2. UNION
3. SELECT column1, column2 ...., columnN FROM table_Name2 [WHERE conditions];
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.
Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Delhi

203 Saket 30000 Aligarh

Table: Employee_details1

Emp Id Emp Name Emp Salary Emp City

203 Saket 30000 Aligarh

204 Saurabh 40000 Delhi

205 Ram 30000 Kerala

201 Sanjay 25000 Delhi

Table: Employee_details2

o Suppose, we want to see the employee name and employee id of each employee from
both tables in a single output. For this, we have to write the following query in SQL:

1. SELECT Emp_ID, Emp_Name FROM Employee_details1


2. UNION
3. SELECT Emp_ID, Emp_Name FROM Employee_details2 ;

SQL Union ALL Operator


The SQL Union Operator is the same as the UNION operator, but the only difference is
that it also shows the same record.

Syntax of UNION ALL Set operator:


1. SELECT column1, column2 ...., columnN FROM table_Name1 [WHERE conditions]

2. UNION ALL
3. SELECT column1, column2 ...., columnN FROM table_Name2 [WHERE conditions];
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.

Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Delhi

203 Saket 30000 Aligarh

Table: Employee_details1

Emp Id Emp Name Emp Salary Emp City

203 Saket 30000 Aligarh

204 Saurabh 40000 Delhi

205 Ram 30000 Kerala

201 Sanjay 25000 Delhi

Table: Employee_details2

o If we want to see the employee name of each employee of both tables in a single output.
For this, we have to write the following query in SQL:

1. SELECT Emp_Name FROM Employee_details1


2. UNION ALL
3. SELECT Emp_Name FROM Employee_details2 ;
SQL Intersect Operator
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.

Syntax of INTERSECT Set operator:


1. SELECT column1, column2 ...., columnN FROM table_Name1 [WHERE conditions]

2. INTERSECT
3. SELECT column1, column2 ...., columnN FROM table_Name2 [WHERE conditions];
Let's understand the below example which explains how to execute INTERSECT
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.

Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Delhi

203 Saket 30000 Aligarh

Table: Employee_details1

Emp Id Emp Name Emp Salary Emp City

203 Saket 30000 Aligarh

204 Saurabh 40000 Delhi

205 Ram 30000 Kerala

201 Sanjay 25000 Delhi

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:
1. SELECT Emp_Name FROM Employee_details1
2. INTERSECT
3. SELECT Emp_Name FROM Employee_details2 ;

SQL Minus Operator


The SQL Minus Operator combines the result of two or more SELECT statements and
shows only the results from the first data set.

Syntax of MINUS operator:


1. SELECT column1, column2 ...., columnN FROM First_tablename [WHERE condition
s]
2. MINUS
3. SELECT column1, column2 ...., columnN FROM Second_tablename [WHERE conditi
ons];
Let's understand the below example which explains how to execute INTERSECT
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.

Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Delhi

203 Saket 30000 Aligarh

Table: Employee_details1
Emp Id Emp Name Emp Salary Emp City

203 Saket 30000 Aligarh

204 Saurabh 40000 Delhi

205 Ram 30000 Kerala

201 Sanjay 25000 Delhi

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:
1. SELECT Emp_Name FROM Employee_details1
2. MINUS
3. SELECT Emp_Name FROM Employee_details2 ;

SQL Unary Operators


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.

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:

1. SQL Unary Positive Operator

2. SQL Unary Negative Operator

3. SQL Unary Bitwise NOT Operator

SQL Unary Positive Operator


The SQL Positive (+) operator makes the numeric value of the SQL table positive.

Syntax of Unary Positive Operator


1. SELECT +(column1), +(column2) ...., +(columnN) FROM table_Name [WHERE cond
itions] ;
Let's understand the below example which explains how to execute a Positive
unary operator on the data of SQL table:

This example consists of anEmployee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.

Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Chandigarh

203 Saket 30000 Delhi

204 Abhay 25000 Delhi

205 Sumit 40000 Kolkata

o Suppose, we want to see the salary of each employee as positive from the
Employee_details table. For this, we have to write the following query in SQL:

1. SELECT +Emp_Salary Employee_details ;

SQL Unary Negative Operator


The SQL Negative (-) operator makes the numeric value of the SQL table negative.

Syntax of Unary Negative Operator


1. SELECT -(column_Name1), -(column_Name2) ...., -(column_NameN) FROM table_
Name [WHERE conditions] ;
Let's understand the below example which explains how to execute Negative
unary operator on the data of SQL table:

This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Emp Id Emp Name Emp Salary Emp City

201 Sanjay 25000 Delhi

202 Ajay 45000 Chandigarh

203 Saket 30000 Delhi

204 Abhay 25000 Delhi

205 Sumit 40000 Kolkata

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:

1. SELECT -Emp_Salary Employee_details ;


o Suppose, we want to see the salary of those employees as negative whose city is
Kolkatain the Employee_details table. For this, we have to write the following query in
SQL:

1. SELECT -Emp_Salary Employee_details WHERE Emp_City = 'Kolkata';

SQL Bitwise NOT Operator


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.

Syntax of Bitwise NOT Operator


1. SELECT ~(column1), ~(column2) ...., ~(columnN) FROM table_Name [WHERE cond
itions] ;
Let's understand the below example which explains how to execute the Bitwise
NOT operator on the data of SQL table:

This example consists of aStudent_details table, which has four columns Roll_No,
Stu_Name, Stu_Marks, and Stu_City.
Emp Id Stu Name Stu Marks Stu City

101 Sanjay 85 Delhi

102 Ajay 97 Chandigarh

103 Saket 45 Delhi

104 Abhay 68 Delhi

105 Sumit 60 Kolkata

If we want to perform the Bitwise Not operator on the marks column


of Student_details, we have to write the following query in SQL:
1. SELECT ~Stu_Marks Employee_details ;

SQL Bitwise Operators


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:

1. Bitwise AND (&)

2. Bitwise OR(|)

Bitwise AND (&)


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.

Syntax of Bitwise AND Operator


1. SELECT column1 & column2 & .... & columnN FROM table_Name [WHERE conditi
ons] ;
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.

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

o Suppose, we want to perform the Bitwise AND operator between both the columns of
the above table. For this, we have to write the following query in SQL:

1. SELECT Column1 & Column2 From TABLE_AND ;

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.

Syntax of Bitwise OR Operator


1. SELECT column1 | column2 | .... | columnN FROM table_Name [WHERE conditions]
;
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.

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

SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.

o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)
o DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.

o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.

Here are some commands that come under DDL:

o CREATE

o ALTER

o DROP

o TRUNCATE

a. CREATE It is used to create a new table in the database.


Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

Example:
1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DA
TE);
b. DROP: It is used to delete both the structure and record stored in the table.

Syntax
1. DROP TABLE table_name;
Example
1. DROP TABLE EMPLOYEE;
c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.

Syntax:

To add a new column in the table


1. ALTER TABLE table_name ADD column_name COLUMN-definition;
To modify existing column in the table:
1. ALTER TABLE table_name MODIFY(column_definitions....);
EXAMPLE
1. ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
2. ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.

Syntax:
1. TRUNCATE TABLE table_name;
Example:
1. TRUNCATE TABLE EMPLOYEE;
2. Data Manipulation Language
o DML commands are used to modify the database. It is responsible for all form of
changes in the database.

o The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.

Here are some commands that come under DML:

o INSERT

o UPDATE

o DELETE

a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of
a table.

Syntax:
1. INSERT INTO TABLE_NAME
2. (col1, col2, col3,.... col N)
3. VALUES (value1, value2, value3, .... valueN);
Or

INSERT INTO TABLE_NAME


1. VALUES (value1, value2, value3, .... valueN);
For example:
1. INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");
b. UPDATE: This command is used to update or modify the value of a column in the
table.

Syntax:
1. UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [W
HERE CONDITION]
For example:
1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'
c. DELETE: It is used to remove one or more row from a table.

Syntax:
1. DELETE FROM table_name [WHERE condition];
For example:
1. DELETE FROM javatpoint
2. WHERE Author="Sonoo";

3. Data Control Language


DCL commands are used to grant and take back authority from any database user.

Here are some commands that come under DCL:

o Grant

o Revoke

a. Grant: It is used to give user access privileges to a database.

Example
1. GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
b. Revoke: It is used to take back permissions from the user.

Example
1. REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

4. Transaction Control Language


TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.

These operations are automatically committed in the database that's why they cannot
be used while creating tables or dropping them.

Here are some commands that come under TCL:

o COMMIT

o ROLLBACK
o SAVEPOINT

a. Commit: Commit command is used to save all the transactions to the database.

Syntax:
1. COMMIT;
Example:
1. DELETE FROM CUSTOMERS
2. WHERE AGE = 25;
3. COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.

Syntax:
1. ROLLBACK;
Example:
1. DELETE FROM CUSTOMERS
2. WHERE AGE = 25;
3. ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling
back the entire transaction.

Syntax:
1. SAVEPOINT SAVEPOINT_NAME;

5. Data Query Language


DQL is used to fetch the data from the database.

It uses only one command:

o SELECT

a. SELECT: This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.

Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:
1. SELECT emp_name
2. FROM employee
3. WHERE age > 20;

Select statement with Clauses( Order By, Group By, Having,


Where)
ORDER BY clause is used to sort the result set either in ascending or descending order
and GROUP BY the statement is used to group the rows that have the same value and it
is used with aggregate functions like SUM(), AVG(), MAX(), etc. In this article, we are
going to discuss both of these clauses in detail. Let’s start with Order by first.
Order By
Order by keyword sort the result-set either in ascending or descending order. This
clause sorts the result set in ascending order by default. In order to sort the result-set in
descending order DESC keyword is used.
Syntax
SELECT column_1, column_2, column_3………..
FROM Table_Name
ORDER BY column_1, column_2, column_3……. ASC|DESC;
Table_Name: Name of the table.
ASC: keyword for ascending order.
DESC: keyword for descending order.
employees table creation –
Here, we create an employees table with employee_id,first_name, and salary and with
the help of order by clause, we can sort the element by their specific column.
Query
create table employees(
employee_id INT PRIMARY KEY ,
first_name VARCHAR(50) ,
salary INT);
INSERT INTO employees(employee_id,first_name,salary)
VALUES
(100,'Steven',24000),
(101,'Neena',17000),
(102,'Lex',17000),
(103,'John',11000),
(104,'Robert',12000),
(105,'Leo',10000);
Output

employee Table

Query
It sorts the records automatically in ascending order if we want to show the records in
descending order then we use DECS.
select * from employees ORDER BY salary;
Output

Table 1

Query
select * from employees ORDER BY salary DESC;
Output

table 2

Group By
Group by statement is used to group the rows that have the same value. It is used with
aggregate functions for example AVG(), COUNT(), SUM()etc. One thing is to
remember about the group by clause that the tuples are grouped based on the similarity
between the attribute values of tuples.
Syntax
SELECT function_Name(column_1), column_2
FROM Table_Name
WHERE condition
GROUP BY column_1, col umn_2
ORDER BY column_1, column_2;
Function_Name: Name of the aggregate function, for example:
SUM(), AVG(), COUNT() etc.
Table_Name: Name of the table.
Query
The COUNT() function is used when we need to return the total number of rows that
are stored in the database.So the example for the COUNT() function is
SELECT COUNT(Salary) from employee;
Output

table 3
Query
The AVG() function is used when we need to return the average value of the record.
SELECT AVG(Salary) from employees;
Output

table 4

Query
The SUM() function is used when we need to return the total sum of the record.
SELECT SUM(Salary) from employees;
Output

table 5

Difference Between ORDER BY and GROUP BY Clause


GROUP BY ORDER BY

Whereas Order by statement sort the


Group by statement is used to group the rows
result-set either in ascending or
that have the same value.
descending order.

It may be allowed in CREATE VIEW While it does not use in CREATE


statement. VIEW statement.

In select statements, it is always used before While in the select statement, it is


the order by keyword. always used after the group by keyword.

Whereas in order by statement, the


An attribute cannot be in the group by a
attribute can be under aggregate
statement under the aggregate function.
function.

In group by clause, the tuples are grouped Whereas in order by clause, the result
based on the similarity between the attribute set is sorted based on ascending or
values of tuples. descending order.

Group by controls the presentation While order by clause controls the


of tuples(rows). presentation of columns.

What is the “Where Clause”?


Where Clause is used to fetch/filter the records into rows before they are grouped. These data
should specify or meet the mentioned condition. It will select and filter only the required data.
Meanwhile, SQL will also implement ‘and’, ‘or’ and ‘not’ in the Where Clause, similar to the
boolean condition. Operations such as “select, update and delete” are also carried out by this
clause.
Example: Consider a table given below for “BOOKS”

BOOK_ID BOOK_NAME PRICE

89 A 230

90 B 430

91 C 324

92 D 730

Take the “Query” for example

SELECT BOOK_ID, PRICE FROM BOOKS WHERE PRICE > 350

Output for this query can be given as

BOOK_ID PRICE

B 430

D 730

What is the “Having Clause”?


Data from a group is fetched with the help of the “Having” clause. It tries to cater to the
condition expected in the final result. The clause is applicable only with Select statements. The
clause is used in column functions.
Example: Take the table below for BOOKS

BOOK_ID BOOK_NAME LANGUAGE PRICE

89 A Hindi 22000

90 B Hindi 20000

91 C English 25000

Consider, we want the count of books with the count of language > 1. Apply the “Having” clause
to the content in the table.

SELECT COUNT(BOOK_ID), LANGUAGE

FROM BOOKS

GROUP BY LANGUAGE

HAVING COUNT(LANGUAGE) > 1;

The output to this is mentioned below:

COUNT(BOOK_ID) LANGUAGE

2 Hindi

What is the Difference between Where and Having Clause in


SQL?
If “Where” clause is used to filter the records from a table that is based on a specified condition,
then the “Having” clause is used to filter the record from the groups based on the specified
condition. Learn more about what is the difference between Where and Having Clause in SQL
from the table below.
Difference between Where and Having Clause in SQL
Where Clause in SQL Having Clause in SQL

Filter table based data catering to specific condition Group based data under set condition

Applicable without GROUP BY clause Does not function without GROUP BY clause

Row functions Column functions

Select, update and delete statements Only select statement

Applied before GROUP BY clause Used after GROUP BY clause

Used with single row operations such as Upper, Lower Applicable with multiple row functions such as Sum,
and so on count and so on

You might also like