Structured Query Language - SQL
Structured Query Language - SQL
Page 1 of 36
Unit III: Database Management System
Page 2 of 36
Unit III: Database Management System
INTRODUCTION
Structured Query Language (SQL) is a standard language used for
accessing databases.
The Structured Query Language (SQL) is the most popular query
language used by major relational database management systems such
as MySql, ORACLE, SQL Server, etc.
SQL is easy to learn as the statements comprise of descriptive English
words and are not case sensitive.
Querying data
Inserting, updating, and deleting rows in a table
Creating, replacing, altering, and dropping objects (tables)
Controlling access to the database and its objects (tables)
Guaranteeing database consistency and integrity
SQL unifies all of the proceeding tasks in one consistent language.
Page 3 of 36
Unit III: Database Management System
Page 4 of 36
Unit III: Database Management System
Page 5 of 36
Unit III: Database Management System
DATE is the 4 digit year, MM is the 2 digit month and DD is the 2 digit
date. The supported range is '1000-01-01' to '9999-12-31'.
Page 6 of 36
Unit III: Database Management System
SQL commands:
CREATE Database
To create a database, we use the CREATE DATABASE statement as shown
in the following syntax:
CREATE DATABASE databasename;
Example: To create a database called Record, we will type following
command at mysql prompt.
Opening a database
Write the following SQL statement for using/opening the database:
mysql> USE Record;
Database changed
Page 7 of 36
Unit III: Database Management System
CREATE Table
Create table command is used to create a table in SQL. It is a DDL
type of command.
Each table must have at least one column.
Syntax:
CREATE TABLE tablename(
attributename1 datatype constraint,
attributename2 datatype constraint,
:
attributenameN datatype constraint);
Page 8 of 36
Unit III: Database Management System
DESCRIBE Table
We can view the structure of an already created table using the describe
statement.
Syntax:
DESCRIBE tablename;
MySQL also supports the short form DESC of DESCRIBE to get description
of table.
OUTPUT:
+------------+-----------+-----+-----+-------+------+
| Field | Type |Null | Key | Default | Extra |
+------------+-----------+-----+-----+-------+------+
| SAdmNo | int | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
|SName |varchar(25) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| DOB | date | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| SClass | numeric(2) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| Sec | char(2) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| Sfees |double(10,2)| YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
6 rows in set (0.06 sec)
Page 9 of 36
Unit III: Database Management System
ALTER Table
It is quite possible that after creating a table, as you start using it, you
may discover you've forgot to mention any column or constraint or
specified a wrong name for the column.
In such situation you can use the ALTER TABLE statement to alter or
change an existing table by adding, changing, or deleting a column in the
table.
The following statement adds a new column mobile to the student table.
OUTPUT:
Page 10 of 36
Unit III: Database Management System
+------------+-----------+-----+-----+-------+------+
| Field | Type |Null | Key | Default | Extra |
+------------+-----------+-----+-----+-------+------+
| SAdmNo | int | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
|SName |varchar(25) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| DOB | date | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| SClass | numeric(2) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| Sec | char(2) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| Sfees |double(10,2)| YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
| mobile | numeric(12) | YES | | NULL | |
+------------+-----------+-----+-----+-------+------+
7 rows in set (0.06 sec)
Page 11 of 36
Unit III: Database Management System
4. Remove an attribute
Using ALTER, we can remove attributes from a table, as shown in the
below syntax:
Syntax
To remove the attribute mobileno from the table STUDENT, we can write
the following MySQL statement:
mysql> ALTER TABLE STUDENT DROP mobileno;
Query OK, 0 rows affected (0.42 sec)
Page 12 of 36
Unit III: Database Management System
DROP Statement
Sometimes a table in a database or the database itself needs to be
removed. We can use DROP statement to remove a database or a table
permanently from the system. However, one should be very cautious
while using this statement as it cannot be undone.
Syntax to drop a table:
Let us try to remove a database table using the DROP TABLE statement.
Example:
Similarly, you can delete a database using the DROP DATABASE statement.
Example:
Page 13 of 36
Unit III: Database Management System
INSERTION of Records
INSERT INTO statement is used to insert new records in a table.
Syntax
->VALUES (1501,’Ravi’,’2000-01-18’,11,’A’,12000);
Syntax:
NOTE: The values must be given in the same order in which attributes
are written in INSERT command.
Example: Write a command to insert student name, class and fees in
table student.
Page 14 of 36
Unit III: Database Management System
->VALUES (’Raman’,,12,15000);
Here,
col1, col2, ... are the column names of the table table_name from
which we want to retrieve data.
The FROM clause is always written with SELECT clause as it specifies
the name of the table from which data is to be retrieved.
The WHERE clause is optional and is used to retrieve data that meet
specified condition(s).
Example:
1. Display student table information.
Page 15 of 36
Unit III: Database Management System
This will display all information of the particular table (student) in the
database.
2. Retrieve selected columns
The following query displays student’ admission numbers and class of
all the students:
Operators in SQL:
The following are the commonly used operators in SQL
1. Arithmetic Operators +, -, *, /
Relational Operators are used when two values are to be compared and
Page 16 of 36
Unit III: Database Management System
1. Display students' admission no. and name, who are paying below
10000 fees.
SAdmno SName
1250 Qamar
2. Display students' admission no. and name, who are paying above or
equal to 10000 fees.
SAdmno SName
1501 Ravi
1605 Amit
1610 Sumit
1201 Annu
1520 Sunita
1001 Umar
Page 17 of 36
Unit III: Database Management System
The above command, on execution, shall increment the value for all the
rows of the field Fees by 50 and shall display the Admno, Name and Fees
for all the students, increased by 500.
Page 18 of 36
Unit III: Database Management System
Only one NULL value is returned in the results with DISTINCT keyword.
Page 19 of 36
Unit III: Database Management System
SClass
11
10
12
8
Page 20 of 36
Unit III: Database Management System
The operator NOT BETWEEN retrieves the rows, which are not
satisfying the BETWEEN condition.
This operator select values that match any value in the given list.
Page 21 of 36
Unit III: Database Management System
Many a times we want to query find out names starting with ‘A’ or to
find out pin codes starting with ‘11’.
This is called substring pattern matching. We cannot match such
patterns using = operator as we are not looking for exact match.
SQL provides LIKE operator that can be used with WHERE clause to
search for a specified pattern in a column.
Patterns are case – sensitive.
The LIKE operator makes use of the following two wild card characters:
% (percent)— used to represent zero, one, or multiple characters
_ (underscore)— used to represent a single character
The keyword NOT LIKE is used to select rows that do not matching the
specified pattern of characters.
Example:
1. Query displays details of all those students whose name starts with 'A'.
Page 22 of 36
2. Query displays details of all those students whose name ends with 'r'.
Page 23 of 36
-> WHERE Sec NOT IN(‘A’,’B’);
Unit III: Database Management System
Example:
1. Query displays details of all those Students who have not been given
fees. This implies that the fees column will be blank.
2. Query displays names of all the students who have been given fees.
-> WHERE Sec NOT IN(‘A’,’B’);
This implies that the fees column will not be blank.
Page 24 of 36
Unit III: Database Management System
ORDER BY Clause:
-> WHERE Sec NOT IN(‘A’,’B’);
ORDER BY clause is used to display the result of a query in a specific
order (sorted order).
It should be kept in mind that the actual data in the database is not
sorted but only the results of the query are displayed in sorted order.
Example:
1. Query to displays details of all the students in ascending order of their
Name.
Page 25 of 36
Unit III: Database Management System
Update Command
Here,
column1_name, column2_name,... are the names of the columns or
fields of a database table whose values you want to update.
You can also combine multiple conditions using
the AND or OR operators,
Page 27 of 36
Unit III: Database Management System
DELETE Command
Warning: The WHERE clause in the DELETE statement specifies which record
Please remember that this command will delete only row information but not
Example: Suppose the student with admission number 1501 has left the school.
We can use the following MySQL statement to delete that record from the STUDENT
table.
Page 29 of 36
Unit III: Database Management System
Aggregate functions
Page 30 of 36
Unit III: Database Management System
1.SUM():
Example:
mysql> SELECT SUM (SFees)
-> FROM STUDENT;
SUM (SFees)
63500
2.AVG():
AVG (SFees)
10583.33
3.MAX():
Page 31 of 36
Unit III: Database Management System
MAX (SFees)
12000
4.MIN():
MIN (SFees)
8000
5.COUNT():
This function is used to find the number of values (i.e. number of rows) of
a particular column.
A. COUNT (Column_Name):
The COUNT (Column_Name) function returns the number of values (NULL
values will not be counted) of the specified column.
Page 32 of 36
Unit III: Database Management System
COUNT (SClass)
6
B. COUNT (*):
The COUNT (*) function returns the total number of records in a table,
counts NULL values also
COUNT (*)
6
C. COUNT (DISTINCT Column_Name):
The COUNT (DISTINCT column_name) function returns the number of
distinct values of the specified column.
GROUP BY CLAUSE:
Syntax:
SELECT column-names FROM table-name
WHERE condition
GROUP BY column-names
Example:
COUNT(*) SClass
1 8
2 10
2 11
1 12
Page 34 of 36
Unit III: Database Management System
HAVING CLAUSE:
Page 35 of 36
Unit III: Database Management System
Example: Display sum of fees which is more than 8000 for each class
SQL JOINS
Page 36 of 36