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

Unit 3 Chapter-2 SQL (Structured Query Language)

Uploaded by

firegamers419
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 3 Chapter-2 SQL (Structured Query Language)

Uploaded by

firegamers419
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Unit - 3

Chapter - 2

SQL
Structured Query Language

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
Introduction
There are many RDBMS such as MySQL,
Microsoft SQL Server, PostgreSQL, Oracle, etc.
that allow us to create a database consisting
of relations and to link one or more relations
for efficient querying to store, retrieve and
manipulate data on that database.

In this chapter, we will learn how to create,


populate and query database using MySQL.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL
Structured Query Language
Introduction

One has to write application programs to access data


in case of a file system.

However, for database management systems there are


special kind of programming languages called query
language that can be used to access data from the
database.

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.
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL
Structured Query Language
SQL is easy to learn as the statements comprise of
descriptive English words and are not case sensitive.

We can create and interact with a database using


SQL in an efficient and easy way.

SQL provides statements for defining the structure of


the data, manipulating data in the database, declare
constraints and retrieve data from the database in
various ways, depending on our requirements.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Advantages of using SQL
1. Faster Query Processing –
Large amount of data is retrieved quickly and efficiently.

2. No Coding Skills –
For data retrieval, large number of lines of code is not required. The
syntactical rules are not complex in SQL, which makes it a user-friendly language.

3. Standardised Language –
Due to documentation and long establishment over years, it provides a uniform
platform worldwide to all its users.

4. Portable –
It can be used in programs independent of any platform (Operating System, etc).

5. Interactive Language –
Easy to learn and understand, answers to complex queries can be received in
seconds.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Data Definition language
SQL provides commands for defining the
relation schemas, modifying relation schemas
and deleting relations.

These are called Data Definition Language (DDL)


through which the set of relations are
specified, including their schema, data type for
each attribute, the constraints as well as the
security and access related authorisations.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Data Manipulation language

Data Manipulation using a database means


either retrieval (access) of existing data,
insertion of new data, removal of existing data
or modification of existing data in the database.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Data Query language
The Structured Query Language (SQL) has
efficient mechanisms to retrieve data stored in
multiple tables in a MySQL database (or any
other RDBMS).

The user enters the SQL commands called


queries where the specific requirements for data
to be retrieved are provided.

The SQL statement SELECT is used to retrieve


data from the tables in a database and is also
called query statement.
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
Introduction to MySQL
MySQL is a open source relational database management
system (RDBMS). It is pronounced as “My Sequel”.

MySQL was originally founded and developed in Sweden


by David Axmark, Allan Larsson and Michael Widenius,
who had worked together since the 1980s.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Characteristics of MySQL
a) MySQL is released under an open-source license so it is
customizable. It requires no cost or payment for its usage.

b) MySQL has superior speed, is easy to use and is reliable.

c) MySQL uses a standard form of the well-known ANSI-SQL


standards.

d) MySQL is a platform independent application which works


on many operating systems like Windows, UNIX, LINUX
etc. and has compatibility with many languages including
JAVA ,C++, PHP, PERL, etc.

e) MySQL is an easy to install RDBMS and is capable of handling


large data sets.
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
Rules while writing SQL statement
Few rules to follow while writing SQL statements in MySQL:

• SQL is case insensitive. That means name and NAME are same
for SQL.

• Always end SQL statements with a semicolon (;).

• To enter multiline SQL statements, we don’t write ‘;’ after the first
line. We put enter to continue on next line.
The prompt mysql> then changes to ‘->’, indicating that
statement is continued to the next line. After the last line, put ‘;’
and press enter.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Data Types in MySQL
Data type indicates the type of data value that an attribute can
have.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
Contraints
• Constraints are certain types of restrictions on the data
values that an attribute can have.
• They are used to ensure the accuracy and reliability of data.
• it is not mandatory to define constraint for each attribute of a
table.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
1. CREATE Database

To create a database, we use the CREATE DATABASE statement.

Syntax:
CREATE DATABASE databasename;

To create a database called School, we will type following command at


mysql prompt.

mysql> CREATE DATABASE School;

To open the database we type the following command


Syntax: Use databasename;

mysql> USE School;


Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
2. CREATE Table

After creating database, we need to define relations (create tables) in this


database and specify attributes for each relation along with data types
for each attribute.
This is done using the CREATE TABLE statement.

Syntax:
CREATE TABLE tablename(
attributename1 datatype constraint,
attributename2 datatype constraint,
.....
.....
attributenameN datatype constraint);

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
CREATE Table
To create table(s) called Student, Subject and Marks.

Student
Marks
Admno (PK) integer (5)
Sname varchar(15) SrNo (PK) int(3)
Gender char(1) Admno (FK) int(5)
Dob date Sub_Code (FK) int(4)
Fname varchar(15) Marks decimal(6,2)
Contactno bigint(10)

Subject

Sub_Code (PK) int(4)


Subname varchar(20)

PK – Primary key FK – Foreign key


Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
SQL for DATA DEFINITION
Student
Admno Sname Gender Dob Fname Contactno
3154 Ajay Verma M 2004-02-12 Aman Verma 1111111111
4156 Suman Gupta F 2004-09-18 Harish Gupta 2222222222
7890 Raman Kumar M 2004-12-01 Karan Singh 3333333333
9910 Diksha F 2004-05-30 Surinder Thakur 4444444444

Subject Marks
SrNo Admno Sub_Code Marks
Sub_Code Subname
1 3154 027 75.00
027 History
2 3154 029 82.00
029 Geography
3 3154 065 90.00
030 Economics
4 4156 041 80.00
041 Mathematics
5 4156 042 76.00
042 Physics
6 4156 043 81.00
043 Chemistry
7 4156 065 99.00
055 Accountancy
8 9910 030 70.00
065 Informatics Practices
9 9910 055 55.00
10 9910 065 92.00
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
SQL for DATA DEFINITION
CREATE Table
To create table(s) called Student, Subject and Marks. We type
following command at MySQL prompt.

CREATE TABLE STUDENT CREATE TABLE SUBJECT


(ADMNO INT(5) PRIMARY KEY, (SUB_CODE INT(4) PRIMARY KEY,
SNAME VARCHAR(15), SUBNAME VARCHAR(20));
GENDER CHAR(1),
DOB DATE,
FNAME VARCHAR(15),
CONTACTNO BIGINT(10)); CREATE TABLE MARKS
(SRNO INT(3),
ADMNO INT(5),
SUB_CODE INT(4),
MARKS DECOMAL(6,2));

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
SQL for DATA DEFINITION
DESCRIBE Table

We can view the structure of an already created table using the DESC or
DESCRIBE statement.

Syntax:
DESCRIBE tablename;

To view the structure of the table, we will type following command at


mysql prompt.
mysql> describe student;
mysql> describe subject;
mysql> describe marks;

To view the tables in the database, we type the following command.

mysql> SHOW TABLES;

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt Courtesy: NCERT
SQL for DATA DEFINITION
3. ALTER Table

After creating a table we may realize that we need to add/remove an


attribute or to modify the datatype of an existing attribute or to add
constraint in attribute.

In all such cases, we need to change or alter the structure of the table by
using the alter statement.

Syntax:
ALTER TABLE tablename ADD/Modify/DROP attribute1,
attribute2,.. ;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
ALTER Table

Add an attribute to an existing table

Syntax:
ALTER TABLE table_name ADD attribute_name DATATYPE;

e.g.
ALTER TABLE STUDENT ADD AADHARNO VARCHAR(12);

Modify datatype of an attribute

Syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE;

e.g.
ALTER TABLE STUDENT MODIFY SNAME VARCHAR(20);
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
ALTER Table

Remove an attribute

Syntax:
ALTER TABLE table_name DROP attribute;

e.g.
ALTER TABLE STUDENT DROP AADHARNO;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
4. DROP Table
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:


DROP TABLE table_name;

Syntax to drop a database:


DROP DATABASE database_name;
Cautions:
1) Using the Drop statement to remove a database will ultimately remove
all the tables within it.
2) DROP statement will remove the tables or database created by you.
Hence you may apply DROP statement at the end of the chapter
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA MANIPULATION
When we create a table, only its structure is
created but the table has no data. To populate
records in the table, INSERT statement is used.
Similarly, table records can be deleted or
updated using SQL data manipulation
statements.

Data Manipulation using a database means


either retrieval (access) of existing data,
insertion of new data, removal of existing data
or modification of existing data in the
database.
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA MANIPULATION
1. INSERTION of Records

INSERT INTO statement is used to insert new records in a table.

Syntax:
INSERT INTO tablename
VALUES(value 1, value 2,....);

Note: We need not to specify attribute names in insert statement


if there are exactly same number of values in the INSERT statement
as the total number of attributes in the table.

e.g. INSERT INTO STUDENT VALUES(6788, ‘AJAY’, ‘M’, ‘204-02-


09’, ’RAMESH CHAND’, 1111111111)
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA MANIPULATION
If we want to provide values only for some of the attributes in a
table (supposing other attributes having NULL or any other default
value), then we shall specify the attribute name alongside each data
value.

Syntax:
INSERT INTO tablename (column1, column2, ...) VALUES
(value1, value2, ...);

e.g. INSERT INTO STUDENT(ADMNO, SNAME, CONTACTNO)


VALUES(6788, ‘SUMAN’, 2222222222)

All the remaining columns i.e. Gender, DOB and FNAME will store
NULL or DEFAULT values.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA MANIPULATION
2. Data Updation

We may need to make changes in the value(s) of one or more


columns of existing records in a table.

For example, we may require some changes in contact number or


spelling of name, etc. The UPDATE statement is used to make such
modifications in the existing data.

Syntax:

UPDATE table_name
SET attribute1 = value1, attribute2 = value2, ...
WHERE condition;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA MANIPULATION
Update values in one column using criteria ‘where’
e.g.
UPDATE STUDENT
SET SNAME=“AMAN” WHERE ADMNO=5611;

Update values in one column ((for all the rows)


e.g.
UPDATE STUDENT SET SCHOOL=“APS YOL”;

Update values for more than one column


e.g.
UPDATE STUDENT
SET SNAME=“AMAN’, CONTACTNO=9999999999 WHERE ADMNO=5611;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA MANIPULATION
3. Data Deletion
The DELETE statement is used to delete one or more record(s)
from a table.

Syntax:
DELETE FROM table_name
WHERE condition;

Delete one row(s) from table based on criteria


e.g. DELETE FROM STUDENT WHERE ADMNO=5611;
DELETE FROM STUDENT WHERE GENDER=“M”;

Delete all the rows from table


e.g. DELETE FROM STUDENT;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
The Structured Query Language (SQL) has efficient
mechanisms to retrieve data stored in multiple tables
in a MySQL database (or any other RDBMS).

The user enters the SQL commands called queries


where the specific requirements for data to be retrieved
are provided.

The SQL statement SELECT is used to retrieve data


from the tables in a database and is also called query
statement.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
The SQL statement SELECT is used to retrieve data from the
table(s) in a database and the output is also displayed in tabular
form.

Syntax:
SELECT attribute1, attribute2, ...
FROM table_name
WHERE condition

Here, attribute1, attribute2, ... 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).
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
A) Retrieve all the columns
The following query displays details of all the employees:

mysql> SELECT * FROM EMPLOYEE;

B) Retrieve selected columns


The following query displays employee name and salary of all the
employees:

mysql> SELECT ENAME, SALARY FROM EMPLOYEE;

(C) DISTINCT Clause


By default, SQL shows all the data retrieved through query as output.
However, there can be duplicate values. The SELECT statement when
combined with DISTINCT clause, returns records without repetition
(distinct records).

mysql> SELECT DISTINCT DeptId FROM EMPLOYEE;


Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
(D) WHERE Clause
The WHERE clause is used to retrieve data that meet some specified
conditions.

mysql> SELECT ENAME FROM EMPLOYEE WHERE Deptid='D01’;

Relational operators (<, <=, >, >=, !=) to specify conditions.

• Display all the employees who are earning more than 5000 and work in
department with DeptId D04.

SELECT * FROM EMPLOYEE


-> WHERE Salary > 5000 AND DeptId = 'D04’;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
• Displays records of all the employees except Aaliya.

SELECT * FROM EMPLOYEE WHERE NOT Ename = 'Aaliya';

• Displays name and department number of all those employees who are
earning salary between 20000 and 50000 (both values inclusive).

SELECT Ename, DeptId FROM EMPLOYEE


-> WHERE Salary>=20000 AND Salary<=50000;
OR

SELECT Ename, DeptId FROM EMPLOYEE


-> WHERE Salary BETWEEN 20000 AND 50000;

Note: The BETWEEN operator defines the range of values in which the column
value must fall into, to make the condition true.

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
• Display details of all the employees who are working either in DeptId D01,
D02 or D04.

SELECT * FROM EMPLOYEE WHERE DeptId = 'D01' OR DeptId = 'D02’


-> OR DeptId = 'D04’;

OR

SELECT * FROM EMPLOYEE WHERE


-> DeptId IN ('D01', 'D02' , 'D04’);

Note: IN operator compares a value with a set of values and returns true if
the value belongs to that set.

SELECT * FROM EMPLOYEE


-> WHERE DeptId NOT IN('D01', 'D02');

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
(F) ORDER BY Clause

ORDER BY displays records in ascending order of the specified column’s


values.
To display the records in descending order, the DESC (means descending)
keyword needs to be written with that column.

• Display details of all the employees in ascending order of their salaries.

SELECT * FROM EMPLOYEE ORDER BY Salary;

• Display details of all the employees in descending order of their salaries.

SELECT * FROM EMPLOYEE ORDER BY Salary DESC;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
G) Handling NULL Values

SQL supports a special value called NULL to represent a missing,


unassigned, or unknown value.

• Displays details of all those employees who have not been given a bonus.

SELECT * FROM EMPLOYEE WHERE Bonus IS NULL;

• Displays details of all those employees who have been given a bonus.

SELECT * FROM EMPLOYEE WHERE Bonus IS NOT NULL;

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
(H) Substring pattern matching

SQL provides LIKE operator that can be used with WHERE clause to search
for a specified pattern in a column.

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

• Display details of all those employees whose name starts with 'K’.
SELECT * FROM EMPLOYEE WHERE Ename LIKE 'K%';

• Display details of all those employees whose name ends with ‘a’.
SELECT * FROM EMPLOYEE WHERE Ename LIKE ‘%a';

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
• Display names of all the employees containing 'se' as a substring in name.

SELECT * FROM EMPLOYEE WHERE Ename LIKE ‘%se%’;

• Display details of all those employees whose name consists of exactly 5


letters and starts with any letter but has ‘ANYA’ after that.

SELECT * FROM EMPLOYEE WHERE Ename LIKE '_ANYA’;

• Display names of all employees containing 'a' as the second character.

SELECT EName FROM EMPLOYEE WHERE Ename LIKE '_a%';

Designed by: Umesh Pun (PGT IP)


Courtesy: NCERT
APS Yol Cantt

You might also like