0% found this document useful (0 votes)
36 views

Structure D Query Language

The document discusses SQL, including its components and commands. SQL is a standard language used to access and manipulate databases. It allows users to create, retrieve, update and delete data from databases. The key components of SQL discussed are DDL (data definition language) for defining database schemas, DML (data manipulation language) for inserting, modifying and deleting data, DCL (data control language) for controlling data access privileges, and TCL (transaction control language) for managing transactions. Example SQL commands from each component such as CREATE, SELECT, GRANT and COMMIT are provided. Data types and constraints that can be applied to tables such as NOT NULL, UNIQUE and PRIMARY KEY are also summarized.

Uploaded by

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

Structure D Query Language

The document discusses SQL, including its components and commands. SQL is a standard language used to access and manipulate databases. It allows users to create, retrieve, update and delete data from databases. The key components of SQL discussed are DDL (data definition language) for defining database schemas, DML (data manipulation language) for inserting, modifying and deleting data, DCL (data control language) for controlling data access privileges, and TCL (transaction control language) for managing transactions. Example SQL commands from each component such as CREATE, SELECT, GRANT and COMMIT are provided. Data types and constraints that can be applied to tables such as NOT NULL, UNIQUE and PRIMARY KEY are also summarized.

Uploaded by

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

CHAPTER 12

STRUCTURE
D QUERY
LANGUAGE
Introduction to SQL
The Structured Query Language
(SQL) is a standard programming
language to access and manipulate
databases.
SQL allows the user to create, retrieve,
alter, and transfer information among
databases.
It is a language designed for managing
and accessing data in a Relational Data
Base Management System (RDBMS).
There are many versions of SQL.
The original version was developed at
IBM’s Research centre and originally
called as Sequel in early 1970’s.
Later the language was changed to SQL.
In 1986, ANSI (American National
Standard Institute) published an SQL
standard that was updated again in 1992
The latest SQL was released in 2008 and
named as SQL 2008.
Role of SQL
in RDBMS
RDBMS stands for Relational
DataBase Management System.
Oracle, MySQL, MS SQL
Server, IBM DB2 and
Microsoft Access are RDBMS
packages.
SQL is a language used to
access data in such databases.
Database is a collection of
tables that store sets of data
that can be queried for use in
other applications.
A database management
system supports the
development, administration
and use of database platforms.
RDBMS is a type of DBMS
with a row-based table
structure that connects related
data elements and includes
functions related to Create,
Read, Update and Delete
operations, collectively known
as CRUD.
The data in RDBMS, is
stored in database objects,
called Tables.
A table is a collection of
related data entries and it
consist of rows and
columns.
A field is a column in a table that
is designed to maintain specific
related information about every
record in the table.
It is a vertical entity that
contains all information
associated with a specific field in
a table.
A Record is a row, which is a
collection of related fields or
columns that exist in a table.
A record is a horizontal
entity in a table which
represents the details of a
particular student in a
student table.
Processing
Skills of SQL
The various processing skills of SQL are :
1.Data Definition Language (DDL) :
The SQL DDL provides commands for
defining relation schemas (structure),
deleting relations, creating indexes and
modifying relation schemas.
2. Data Manipulation Language (DML) :
The SQL DML includes commands to
insert, delete, and modify tuples in the
database.
3. Embedded Data Manipulation Language :
The embedded form of SQL is used in high
level programming languages.
4. View Definition :
The SQL also includes commands for
defining views of tables.
5. Authorization :
The SQL includes commands for access
rights to relations and views of tables.
6. Integrity :
The SQL provides forms for
integrity checking using
condition.
7. Transaction control :
The SQL includes commands for
file transactions and control
over transaction processing.
Creating
Database
Components
of SQL
DML
Data Manipulation Language
DDL
Data Definition Language
DCL
Data Control Language
TCL
Transaction Control Language
DQL
Data Query Language
DML
Data Manipulation
Language
A Data Manipulation Language (DML) is a
computer programming language used for
adding (inserting), removing (deleting), and
modifying (updating) data in a database.
In SQL, the data manipulation language
comprises the SQL-data change statements,
which modify stored data but not the schema
of the database table.
After the database schema has been specified
and the database has been created, the data
can be manipulated using a set of procedures
which are expressed by DML.
By Data Manipulation we mean,
• Insertion of new information into
the database
• Retrieval of information stored in
a database.
• Deletion of information from the
database.
• Modification of data stored in the
database.
The DML is basically of two
types:
• Procedural DML – Requires a user to
specify what data is needed and
how to get it.
• Non-Procedural DML - Requires a
user to specify what data is needed
without specifying how to get it.
DML COMMANDS
Insert
Inserts data into a table
Update
Updates the existing data within a
table.
Delete
Deletes all records from a table, but
not the space occupied by them.
DDL
Data Definition
Language
The Data Definition Language
(DDL) consist of SQL statements
used to define the database
structure or schema.
It simply deals with descriptions of
the database schema and is used to
create and modify the structure of
database objects in databases.
A DDL performs the following functions :
1. It should identify the type of data division such as
data item, segment, record and database file.
2. It gives a unique name to each data item type,
record type, file type and data base.
3. It should specify the proper data type.
4. It should define the size of the data item.
5. It may define the range of values that a data item
may use.
6. It may specify privacy locks for preventing
unauthorized data entry.
DDL COMMANDS
Create
To create tables in the database.
Alter
Alters the structure of the database.
Drop
Delete tables from database
.

Truncate
Remove all records from a table, also release
the space occupied by those records.
create table excel(id int(11) unsigned
auto_increment primary key not null,
name varchar(25) not null,
gender varchar(15) not null,
mobno int(15) not null,
fname varchar(25) not null,
mname varchar(25) not null);
DCL
Data Control
Language
A Data Control Language (DCL) is a
programming language used to
control the access of data stored in a
database. It is used for controlling
privileges in the database
(Authorization).
The privileges are required for
performing all the database
operations such as creating
sequences, views of tables etc.
DCL COMMANDS
Grant
Grants permission to one or more
users to perform specific tasks.
Revoke
Withdraws the access permission
given by the GRANT statement.
TCL
Transactional Control
Language
Transactional control language
(TCL) commands are used to
manage transactions in the
database.

These are used to manage the


changes made to the data in a
table by DML statements.
TCL COMMANDS
Commit
Saves any transaction into the database
permanently.
Roll back
Restores the database to last commit state.
Save point
Temporarily save a transaction so that you
can rollback.
DQL
Data Query Language
The Data Query
Language consist of
commands used to
query or retrieve data
from a database.
DQL COMMANDS
Select
It displays the
records from the
table.
Data Types
The data in a database is stored
based on the kind of value
stored in it.
This is identified as the data
type of the data or by assigning
each field a data type. All the
values in a given field must be
of same type.
SQL Commands
and their Functions
Tables are the only way to store
data, therefore all the
information has to be arranged
in the form of tables.
The SQL provides a
predetermined set of
commands to work on
databases.
Keywords
They have a special meaning in SQL. They are
understood as instructions.
Commands
They are instructions given by the user to the
database also known as statements.
Clauses
They begin with a keyword and consist of
keyword and argument.
Arguments
They are the values given to make the clause
complete.
DDL COMMANDS
CREATE
TABLE
Command
You can create a table by using
the CREATE TABLE command.
When a table is created, its
columns are named, data types
and sizes are to be specified.
Each table must have at least
one column.
SYNTAX
CREATE TABLE <table-name>
(<column name><data type>[<size>]
(<column name><data type>[<size>]
……
);
CREATE TABLE Student
(Admno integer,
Name char(20),
Gender char(1),
Age integer,
Place char(10),
);
Column constraint:
Column constraint apply only to
individual column.
[Limits only column data.]
Table constraint :
Table constraint apply to a group of
one or more columns.
[Limits whole table data.]
NOT NULL Constraint
NOT NULL constraint restricts a
column from having a NULL value.
Once NOT NULL constraint is
applied to a column, you cannot
pass a null value to that column.
It enforces a column to contain a
proper value.
SYNTAX
CREATE TABLE <table-name>
(<column name><data type>[<size>]<column constraint>,
(<column name><data type>[<size>]<column constraint>
……
<table constraint>(<column name>,[<column name>….])
…..
);
CREATE TABLE Student
(
Admno integer NOT NULL PRIMARY KEY, → Primary
Key constraint
Name char(20)NOT NULL,
Gender char(1),
Age integer,
Place char(10),
);
Type of
Constraints
Constraints ensure database
integrity, therefore known as
database integrity constraints.
The different types of
constraints are :
Unique
CONSTRAIN
T
This constraint
ensures that no
two rows have the
same value in the
specified columns.
CREATE TABLE Student
(Admno integer NOT NULL
UNIQUE, → Unique constraint
Name char (20) NOT NULL,
Gender char (1),
Age integer,
Place char (10),
);
The UNIQUE constraint can be applied only to
fields that have also been declared as NOT
NULL.
When two constraints are applied on a single
field, it is known as multiple constraints.
In the above Multiple constraints NOT NULL and
UNIQUE are applied on a single field Admno, the
constraints are separated by a space and at the
end of the field definition a comma(,) is added.
By adding these two constraints the field
Admno must take some value ie. will not be
NULL and should not be duplicated.
PRIMARY
KEY
CONSTRAIN
This constraint declares a field as a
Primary key which helps to uniquely
identify a record.
It is similar to unique constraint
except that only one field of a table
can be set as primary key.
The primary key does not allow NULL
values and therefore a field declared
as primary key must have the NOT
NULL constraint.
CREATE TABLE Student(
Admno integer NOT NULL PRIMARY KEY, →
Primary Key constraint
Name char(20)NOT NULL,
Gender char(1),
Age integer,
Place char(10),
);
DEFAULT
CONSTRAIN
T
The DEFAULT constraint is used
to assign a default value for the
field. When no value is given
for the specified field having
DEFAULT constraint,
automatically the default value
will be assigned to the field.
CREATE TABLE Student
(
Admno integer NOT NULL PRIMARY KEY,
Name char(20)NOT NULL,
Gender char(1),
Age integer DEFAULT = “17”, → Default
Constraint
Place char(10),
);
CHECK
CONSTRAIN
T
This constraint helps to set a
limit value placed for a field.
When we define a check
constraint on a single
column, it allows only the
restricted values on that
field.
CREATE TABLE Student
(
Admno integer NOT NULL PRIMARY KEY
Name char(20)NOT NULL,
Gender char(1),
Age integer (CHECK<=19), → Check
Constraint
Place char(10),
);
TABLE
CONSTRAIN
T
When the constraint is
applied to a group of fields
of the table, it is known as
Table constraint.
The table constraint is
normally given at the end of
the table definition
DML
COMMANDS
Once the schema or
structure of the table is
created, values can be added
to the table.
The DML commands consist
of inserting, deleting and
updating rows into the table.
DML COMMANDS
Insert
Inserts data into a table
Update
Updates the existing data within a
table.
Delete
Deletes all records from a table, but
not the space occupied by them.
INSERT
The COMMANDS
INSERT command helps
to add new data to the
database or add new records
to the table. The command is
used as follows:
SYNTAX

INSERT INTO <table-name>


[column-list] VALUES (values);
insert into Student
(name,gender,age,place) values
(‘Ramesh','male',37,'marthandam')
;
DELETE
The COMMAND
DELETE command
permanently removes one or
more records from the table.
It removes the entire row, not
individual fields of the row, so
no field argument is needed
DELETE FROM table-name
WHERE condition;
delete from ebi where
name='excel';
To delete all the rows of
the table, the command
is used as :
DELETE * FROM Student;
UPDATE COMMAND
The UPDATE command updates some
or all data values in a database.
It can update one or more records in a
table.
The UPDATE command specifies the
rows to be changed using the WHERE
clause
and the new data using the SET
keyword.
SYNTAX
UPDATE <table-name> SET column-
name = value, column-name = value,…
WHERE condition;
+----+--------+--------+--------+----------+-------+--------------+
| id | name | gender | mobno | fname | mname |
+----+--------+--------+--------+----------+-------+--------------+
| 1 | sunesh | male | 99988 | stanly | leela |
| 2 | ramesh | male | 654123 | chandran | lily |
| 3 | geetha | female | 654123 | mahesh | lotus |
+----+--------+--------+--------+----------+-------+--------------+
update excel set
mobno=123456 where
id=1;
+----+--------+--------+--------+----------+-------+-------+
| id | name | gender | mobno | fname | mname |
+----+--------+--------+--------+----------+-------+-------+
| 1 | sunesh | male | 123456 | stanly | leela |
| 2 | ramesh | male | 654123 | chandran | lily |
| 3 | geetha | female | 654123 | mahesh | lotus |
+----+--------+--------+--------+----------+-------+-------+
update excel set
mobno=999999 where
name='sunesh';
+----+---------+--------+--------+----------+-------+-------+
| id | name | gender | mobno | fname | mname |
+----+---------+--------+--------+----------+-------+-------+
| 1 | sunesh | male | 999999 | stanly | leela |
| 2 | ramesh | male | 654123 | chandran | lily |
| 3 | geetha | female | 654123 | mahesh | lotus |
| 4 | ebenson | male | 654123 | sunesh | jonie |
+----+---------+--------+--------+----------+-------+-------+
ALTER
COMMAND
The ALTER command is used to
alter the table structure like adding
a column, renaming the existing
column, change the data type of
any column or size of the column
or delete the column from the
table.
alter table excel add age
int(10);
insert into
excel(name,gender,mobno,fname,mname,
age)values('ebenson','mal
e',654123,'sunesh','jonie',17);
+----+---------+--------+--------+----------+-------+------+
| id | name | gender | mobno | fname | mname | age |
+----+---------+--------+--------+----------+-------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
+----+---------+--------+--------+----------+-------+------+
TRUNCATE
command
The TRUNCATE command is
used to delete all the rows
from the table, the
structure remains and the
space is freed from the
table.
SYNTAX
TRUNCATE TABLE table-name;
DROP TABLE
The DROP TABLE command is used to
command
remove a table from the database.
If you drop a table, all the rows in the table
is deleted and the table structure is
removed from the database.
Once a table is dropped we cannot get it
back, so be careful while using DROP TABLE
command.
But there is a condition for dropping a
table; it must be an empty table.
SYNTAX

DROP TABLE table-name;


DQL COMMAND–
SELECT command
One of the most important tasks when
working with SQL is to generate Queries
and retrieve data.
A Query is a command given to get a
desired result from the database table.
The SELECT command is used to query or
retrieve data from a table in the database.
It is used to retrieve a subset of records
from one or more tables.
SYNTAX
SELECT <column
list>FROM<table-name>;
Table-name is the name of the table
from which the information is
retrieved.
Column-list includes one or more
columns from which data is retrieved.
To view all the fields and rows of
the table the SELECT command can
be given as
SELECT * FROM STUDENT;
DISTINCT Keyword
The DISTINCT keyword is used
along with the SELECT command to
eliminate duplicate rows in the
table. This helps to eliminate
redundant data.
SELECT DISTINCT
field name FROM
Table;
+----+---------+--------+--------+----------+-------+------+
| id | name | gender | mobno | fname | mname | age |
+----+---------+--------+--------+----------+-------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
| 6 | hello | male | 321456 | thankam | rose | 12 |
+----+---------+--------+--------+----------+-------+------+
select distinct gender from excel;
+--------+
| gender |
+--------+
| male |
| female |
+--------+
ALL Keyword
The ALL keyword retains
duplicate rows. It will
display every row of the
table without considering
duplicate entries.
+----+---------+--------+--------+----------+-------+------+
| id | name | gender | mobno | fname | mname | age |
+----+---------+--------+--------+----------+-------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
| 6 | hello | male | 321456 | thankam | rose | 12 |
+----+---------+--------+--------+----------+-------+------+
select all gender from excel;
+--------+
| gender |
+--------+
| male |
| male |
| female |
| male |
| male |
| male |
+--------+
The WHERE clause in the SELECT
command specifies the criteria for getting
the desired result. The general form of
SELECT command with WHERE Clause is:

SELECT <column-name>[,<column-
name>,….] FROM <table-
name>WHERE condition>;
+----+---------+--------+--------+----------+-------+------+
| id | name | gender | mobno | fname | mname | age |
+----+---------+--------+--------+----------+-------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
| 6 | hello | male | 321456 | thankam | rose | 12 |
+----+---------+--------+--------+----------+-------+------+
select name, gender from excel where
fname='sunesh';
+---------+--------+
| name | gender |
+---------+--------+
| ebenson | male |
| ebenson | male |
+---------+--------+
select * from excel;
+----+-------------+--------+--------+----------+--------+------+
| id | name | gender | mobno | fname | mname | age |
+----+-------------+--------+--------+----------+--------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
| 6 | hello | male | 321456 | thankam | rose | 12 |
| 7 | friend | male | 321456 | rrr | rtttte | 22 |
| 8 | britto | male | 321456 | rrr | rtttte | 92 |
| 9 | ramesh babu | male | 321456 | rrr | rtttte | 10 |
+----+-------------+--------+--------+----------+--------+------+
select name, gender from excel where age >=10;
+-------------+--------+
| name | gender |
+-------------+--------+
| ebenson | male |
| hello | male |
| friend | male |
| britto | male |
| ramesh babu | male |
+-------------+--------+
select * from excel;
+----+-------------+--------+--------+----------+--------+------+
| id | name | gender | mobno | fname | mname | age |
+----+-------------+--------+--------+----------+--------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
| 6 | hello | male | 321456 | thankam | rose | 12 |
| 7 | friend | male | 321456 | rrr | rtttte | 22 |
| 8 | britto | male | 321456 | rrr | rtttte | 92 |
| 9 | ramesh babu | male | 321456 | rrr | rtttte | 10 |
+----+-------------+--------+--------+----------+--------+------+
select name, gender from excel where age <=10;
+-------------+--------+
| name | gender |
+-------------+--------+
| ramesh babu | male |
+-------------+--------+
select * from excel;
+----+-------------+--------+--------+----------+--------+------+
| id | name | gender | mobno | fname | mname | age |
+----+-------------+--------+--------+----------+--------+------+
| 1 | sunesh | male | 999999 | stanly | leela | NULL |
| 2 | ramesh | male | 654123 | chandran | lily | NULL |
| 3 | geetha | female | 654123 | mahesh | lotus | NULL |
| 4 | ebenson | male | 654123 | sunesh | jonie | NULL |
| 5 | ebenson | male | 654123 | sunesh | jonie | 17 |
| 6 | hello | male | 321456 | thankam | rose | 12 |
| 7 | friend | male | 321456 | rrr | rtttte | 22 |
| 8 | britto | male | 321456 | rrr | rtttte | 92 |
| 9 | ramesh babu | male | 321456 | rrr | rtttte | 10 |
+----+-------------+--------+--------+----------+--------+------+
SELECT name, age FROM excel WHERE (age>=18 or
fname ='rrr');
+-------------+------+-------------
| name | age |
+-------------+------+-------------
| friend | 22 |
| britto | 92 |
| ramesh babu | 10 |
+-------------+------+-------------
BETWEEN and NOT BETWEEN
Keywords
The BETWEEN keyword defines a
range of values the record must
fall into to make the condition
true.
The range may include an upper
value and a lower value between
which the criteria must fall into.
The NOT BETWEEN is
reverse of the
BETWEEN operator
where the records not
satisfying the condition
are displayed.
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| first | varchar(15) | NO | | NULL | |
| last | varchar(20) | NO | | NULL | |
| age | int(3) | NO | | NULL | |
| address | varchar(30) | NO | | NULL | |
| city | varchar(20) | NO | | NULL | |
| state | varchar(20) | NO | | NULL | |
| product_name | varchar(20) | NO | | NULL | |
| unit_price | varchar(20) | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
| id | first | last | age | address | city | state | product_name | unit_price |
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
| 1 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| hundred |
| 2 | rose | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 3 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| 500 |
| 4 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| 900 |
| 5 | Excel | Hello | 42 | thiruvattar | marthandam | tamil nadu | Lux| 900 |
| 6 | Excel | Hello | 42 | thiruvattar | marthandam | tamil nadu | Lux| 850 |
| 7 | cabbage | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 8 | lily | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 9 | mango | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
SELECT id, product_name,unit_price
FROM employee
WHERE unit_price BETWEEN 500 AND 900
ORDER BY unit_price
+----+--------------+------------+
| id | product_name | unit_price |
+----+--------------+------------+
| 3 | Lux | 500 |
| 6 | Lux | 850 |
| 4 | Lux | 900 |
| 5 | Lux | 900 |
+----+--------------+------------+
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
| id | first | last | age | address | city | state | product_name | unit_price |
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
| 1 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| hundred |
| 2 | rose | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 3 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| 500 |
| 4 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| 900 |
| 5 | Excel | Hello | 42 | thiruvattar | marthandam | tamil nadu | Lux| 900 |
| 6 | Excel | Hello | 42 | thiruvattar | marthandam | tamil nadu | Lux| 850 |
| 7 | cabbage | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 8 | lily | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 9 | mango | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
SELECT id, product_name,unit_price
FROM employee
WHERE unit_price NOT BETWEEN 500 AND
900
ORDER BY unit_price
+----+--------------+------------+
| id | product_name | unit_price |
+----+--------------+------------+
| 2 | Lux | 2000 |
| 7 | Lux | 2000 |
| 8 | Lux | 2000 |
| 9 | Lux | 2000 |
| 1 | Lux | hundred |
+----+--------------+------------+
IN Keyword
The IN keyword is used to specify
a list of values which must be
matched with the record values.
In other words it is used to
compare a column with more
than one value. It is similar to an
OR condition.
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
| id | first | last | age | address | city | state | product_name | unit_price |
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
| 1 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| hundred |
| 2 | rose | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 3 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| 500 |
| 4 | Excel | Hello | 12 | thiruvattar | marthandam | tamil nadu | Lux| 900 |
| 5 | Excel | Hello | 42 | thiruvattar | marthandam | tamil nadu | Lux| 900 |
| 6 | Excel | Hello | 42 | thiruvattar | marthandam | tamil nadu | Lux| 850 |
| 7 | cabbage | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 8 | lily | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
| 9 | mango | flower | 55 | thiruvattar | marthandam | tamil nadu | Lux| 2000 |
+----+---------+--------+-----+-------------+------------+------------+--------------+------------+
SELECT id, first FROM
employee WHERE last
IN (“flower ”, “Hello ”);
+----+---------+
| id | first |
+----+---------+
| 1 | Excel |
| 2 | rose |
| 3 | Excel |
| 4 | Excel |
| 5 | Excel |
| 6 | Excel |
| 7 | cabbage |
| 8 | lily |
| 9 | mango |
+----+---------+
SELECT id, first FROM
employee WHERE last
IN (“flower ”);
+----+---------+
| id | first |
+----+---------+
| 2 | rose |
| 7 | cabbage |
| 8 | lily |
| 9 | mango |
+----+---------+
ORDER BY clause
The ORDER BY clause in SQL is used to sort
the data in either ascending or descending
based on one or more columns.
1. By default ORDER BY sorts the data in
ascending order.
2. We can use the keyword DESC to sort
the data in descending order and the
keyword ASC to sort in ascending order.
create table mouse
(id int(11) unsigned auto_increment
primary key not null,
name varchar(15) not null,
age varchar(20) not null,
gender varchar(3)not null);
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 4 | akshay | 17 | mal |
| 5 | viji | 17 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 8 | femi | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
select * from mouse order by name;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 4 | akshay | 17 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 1 | Excel | 23 | mal |
| 8 | femi | 18 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 9 | ramesh | 98 | mal |
| 5 | viji | 17 | fem |
+----+--------+-----+--------+
WHERE clause
The WHERE clause is used
to filter the records. It
helps to extract only
those records which
satisfy a given condition.
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 4 | akshay | 17 | mal |
| 5 | viji | 17 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 8 | femi | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
select * from mouse where age>=18 order by name;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 8 | femi | 18 | fem |
| 7 | mala | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 4 | akshay | 17 | mal |
| 5 | viji | 17 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 8 | femi | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
select * from mouse order by id desc;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 9 | ramesh | 98 | mal |
| 8 | femi | 18 | fem |
| 7 | mala | 18 | fem |
| 6 | kala | 17 | fem |
| 5 | viji | 17 | fem |
| 4 | akshay | 17 | mal |
| 3 | eric | 17 | mal |
| 2 | athira | 17 | fem |
| 1 | Excel | 23 | mal |
+----+--------+-----+--------+
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 4 | akshay | 17 | mal |
| 5 | viji | 17 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 8 | femi | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
select * from mouse order by name desc;
| id | name | age | gender |
+----+--------+-----+--------+
| 5 | viji | 17 | fem |
| 9 | ramesh | 98 | mal |
| 7 | mala | 18 | fem |
| 6 | kala | 17 | fem |
| 8 | femi | 18 | fem |
| 1 | Excel | 23 | mal |
| 3 | eric | 17 | mal |
| 2 | athira | 17 | fem |
| 4 | akshay | 17 | mal |
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 4 | akshay | 17 | mal |
| 5 | viji | 17 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 8 | femi | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
select * from mouse order by name
asc;
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 4 | akshay | 17 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 1 | Excel | 23 | mal |
| 8 | femi | 18 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 9 | ramesh | 98 | mal |
| 5 | viji | 17 | fem |
+----+--------+-----+--------+
GROUP BY clause
The GROUP BY clause is used
with the SELECT statement to
group the students on rows or
columns having identical
values or divide the table in to
groups.
The GROUP BY clause is
a SQL command that is
used to group rows
that have the same
values.
SYNTAX
SELECT <column-names> FROM
<table-name> GROUP BY
<column-name>HAVING
condition];
+----+--------+-----+--------+
| id | name | age | gender |
+----+--------+-----+--------+
| 1 | Excel | 23 | mal |
| 2 | athira | 17 | fem |
| 3 | eric | 17 | mal |
| 4 | akshay | 17 | mal |
| 5 | viji | 17 | fem |
| 6 | kala | 17 | fem |
| 7 | mala | 18 | fem |
| 8 | femi | 18 | fem |
| 9 | ramesh | 98 | mal |
+----+--------+-----+--------+
select gender from mouse group by
gender;
+--------+
| gender |
+--------+
| fem |
| mal |
+--------+
select age,gender from mouse group
by age,gender;
+-----+--------+
| age | gender |
+-----+--------+
| 17 | fem |
| 17 | mal |
| 18 | fem |
| 23 | mal |
| 98 | mal |
+-----+--------+
For example to count the
number of male and female
students in the student
table, the following
command is given :
select gender,count(*) from mouse
group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| fem | 5|
| mal | 4|
+--------+----------+
HAVING clause
The HAVING clause can be
used along with GROUP BY
clause in the SELECT statement
to place condition on groups
and can include aggregate
functions on them.
The following are the most commonly used
SQL aggregate functions:
AVG 
– calculates the average of a set of values.
COUNT 
– counts rows in a specified table or view.
MIN 
– gets the minimum value in a set of values.
MAX 
– gets the maximum value in a set of values.
SUM 
– calculates the sum of values.
create table hello (id int(11)
unsigned auto_increment primary
key not null,
name varchar(15) not null,
age varchar(20) not null,
gender varchar(3)not null,
place varchar(10) not null);
select * from hello;
+----+----------+-----+--------+------------+
| id | name | age | gender | place |
+----+----------+-----+--------+------------+
| 1 | sunesh | 39 | mal | marthandam |
| 2 | ramesh | 49 | mal | chennai |
| 3 | kala | 49 | fem | chennai |
| 4 | mala | 39 | fem | madurai |
| 5 | suresh | 99 | mal | bangalore |
| 6 | mahesh | 39 | mal | bangalore |
| 7 | gala | 49 | fem | chennai |
| 8 | ragimala | 29 | fem | madurai |
+----+----------+-----+--------+------------+
For example to count the number of Male and
Female students belonging to Chennai .
select count(id),place from hello group by
place having count(id) >2;
+-----------+---------+
| count(id) | place |
+-----------+---------+
| 3 | chennai |
+-----------+---------+
select count(id),place from hello group by
place having count(id) >1;
+-----------+-----------+
| count(id) | place |
+-----------+-----------+
| 2 | bangalore |
| 3 | chennai |
| 2 | madurai |
+-----------+-----------+
select max(id),place from hello group by
place having max(id) >=1;
+---------+------------+
| max(id) | place |
+---------+------------+
| 6 | bangalore |
| 7 | chennai |
| 8 | madurai |
| 1 | marthandam |
+---------+------------+
select sum(id),place from hello group
by place having sum(id) >1;
+---------+-----------+
| sum(id) | place |
+---------+-----------+
| 11 | bangalore |
| 12 | chennai |
| 12 | madurai |
+---------+-----------+
select min(id),place from hello group
by place having min(id) >1;
+---------+-----------+
| min(id) | place |
+---------+-----------+
| 5 | bangalore |
| 2 | chennai |
| 4 | madurai |
+---------+-----------+
SELECT COMMAND
COMMAN
D
SELECT <column-
list>FROM<table-name>;
EXAMPL
E
SELECT age FROM excel;
COMMAN
D
SELECT * FROM <table-name>;

EXAMPL
E
SELECT * FROM excel;
COMMAN
D
SELECT DISTINCT <column-list>
FROM <table-name>;

EXAMPL
E SELECT DISTINCT age FROM
excel;
COMMAN
D
SELECT ALL <column-list>FROM
<table-name>;

EXAMPL
ESELECT ALL age FROM excel;
COMMAN
D
SELECT <column-name>[,<column-
name>,….] FROM <table-name>
WHERE condition>;
EXAMPL
E
SELECT Admno, Name, Place FROM
excel WHERE Place =”Chennai”;
COMMAN
DSELECT <column-list>,<column-list> FROM
<table-name>;
WHERE <BETWEEN <column-list>AND
<column-list>;

EXAMPL
E SELECT Admno, Name, Age FROM
excel WHERE Age BETWEEN 18 AND
19;
COMMAN
DSELECT <column-list>,<column-list> FROM
<table-name>;
WHERE <column-list> NOT BETWEEN <column-
list>AND <column-list>;

EXAMPL
E SELECT Admno, Name, Age FROM
excel WHERE Age NOT BETWEEN 18
AND 19;
COMMAN
D
SELECT <column-list>, <column-list>, <column-
list>FROM <table-name> WHERE <column-
list>IN (“<column-list>”, “<column-list>”);

EXAMPL
ESELECT Admno, Name, Place FROM
excel WHERE Place IN (“Chennai”,
“Delhi”);
COMMAN
D
SELECT <column-list>, <column-list>, <column-
list>FROM <table-name> WHERE <column-list>
NOT IN (“<column-list>”, “<column-list>”);

EXAMPL
ESELECT Admno, Name, Place FROM
excel WHERE Place NOT IN (“Chennai”,
“Delhi”);
COMMAN
D
SELECT * FROM <table-name> WHERE
<column-list>IS NULL;

EXAMPL
E
SELECT * FROM excel WHERE Age IS NULL;
COMMAN
D
SELECT <column-name>[,<column-name>,
….] FROM <table-name>ORDER BY
<column1>,<column2>,… desc;

EXAMPL
Eselect * from excel order by id
desc;
COMMAN
D
SELECT <column-name>[,<column-name>,
….] FROM <table-name>ORDER BY
<column1>,<column2>,…ASC;

EXAMPL
Eselect * from excel order by id
ASC;
COMMAN
D SELECT * FROM <table-
name>ORDER BY <column-list>;

EXAMPL
E
SELECT * FROM excel ORDER
BY Name;
COMMAN
D SELECT * FROM <table-
name>WHERE <column-list>
ORDER BY <column-list>;
EXAMPL
E
SELECT * FROM excel WHERE Age>=18
ORDER BY Name
COMMAN
D SELECT * FROM <table-
name>WHERE <column-list>
ORDER BY <column-list>ASC;
EXAMPL
E
SELECT * FROM excel WHERE Age>=18
ORDER BY Name ASC;
COMMAN
D SELECT * FROM <table-
name>WHERE <column-list>
ORDER BY <column-list> DESC;
EXAMPL
E
SELECT * FROM excel WHERE Age>=18
ORDER BY Name DESC;
COMMAN
DSELECT <column-names> FROM
<table-name> GROUP BY <column-
name> HAVING condition];
EXAMPL
Eselect count(id),place from excel
group by place having count(id) >1;
COMMAN
D
SELECT <column-names>, count(*)
FROM <table-name> GROUP BY
<column-names>;
EXAMPL
E SELECT Gender, count(*) FROM
excel GROUP BY Gender;
COMMAN
DSELECT <column-names>, count(*)
FROM Student GROUP BY <column-
names> HAVING <column-names>
=<condition>;
EXAMPL
ESELECT Gender , count(*) FROM
excel GROUP BY Gender HAVING
Place = ‘Chennai’;
INSERT COMMAND
COMMAN
D
INSERT INTO <table-name> [column-
list] VALUES (values);
EXAMPL
E
INSERT INTO excel (Admno, Name,
Gender, Age, Place)
VALUES (100,’ Ashish’,’ M’, 17,’
Chennai’);
DELETE
COMMAND
COMMAN
D DELETE FROM table-name WHERE
condition;

EXAMPL
E
DELETE FROM excel WHERE
Admno=104;
COMMAN
D DELETE * FROM <TABLE-
NAME>;
EXAMPL
E
DELETE * FROM excel ;
UPDATE
COMMAND
COMMAN
DUPDATE <table-name> SET column-
name = value, column-name = value,…
WHERE condition;
EXAMPL
EUPDATE excel SET Age = 20
WHERE Place =
“Bangalore”;
ALTER COMMAND
COMMAN
D ALTER TABLE <table-name> ADD
<column-name><data type><size>;

EXAMPL
E
ALTER TABLE excel ADD
Address char;
DROP TABLE
COMMAND
COMMAN
D
DROP TABLE table-name;

EXAMPL
E
DROP TABLE excel;
COMMIT
COMMAND
COMMAN
D
COMMIT

EXAMPL
E
COMMIT
ROLLBACK
COMMAND
COMMAN
D
ROLL BACK TO
SAVE POINT NAME
SAVE POINT
COMMAND
COMMAN
D
SAVE POINT
savepoint name
PART IV -2
(i) SELECT * FROM Employee ORDER BY PAY
DESC;
(ii) SELECT * FROM Employee WHERE
ALLOWANCE BETWEEN 5000 AND 7000;
(iii) DELETE FROM Employee WHERE DESIG
='Mechanic';
(iv) INSERT INTO Employee VALUES ('c1005',
'Abhi', 'Mechanic', 15000, 6500);
(v) SELECT * FROM Employee WHERE DESIG
='Operator';
PART IV -5
CREATE TABLE employee
(
empcode integer NOTNULL,
efirstname char(20),
elastname char(20),
Designation char(20),
Pay integer,
PRIMARY KEY (efirstname, elastname)
);

You might also like