10/5/2024
Structured Query Language (SQL)
Chapter 8
What is SQL?
• SQL stands for Structured Query Language.
• It is a standard language used for accessing and manipulating databases.
• SQL commands are helpful in managing Relational Database Management
Systems (RDBMS)
• SQL is used to perform a variety of operations on data, including creating,
retrieving, updating, and deleting data.
• SQL was developed in 1970 by Donald D. Chamberlin and Raymond F.
Boyce at IBM
1
10/5/2024
Features of SQL
SQL has the following salient features and strong processing capabilities:
• It can retrieve data from a database through Query processing.
• It can insert records into a database.
• It can update records in a database.
• It can create new databases and modify the existing ones.
• It can create new tables in a database.
• It can create views in a database.
• It can allow modifying security settings for the system
Advantages of SQL
1. Ease of use: It is very easy to learn and use and does not require high-end
professional training to work upon it.
2. Large volume of data can be handled quite easily.
3. No coding required: It is non-procedural and a unified language, i.e., we need not
specify the procedures to accomplish a task but only need to give a command to
perform the activity.
4. SQL can be linked to most of the other high-level languages which makes it the first
choice for database programmers.
5. Portable: It is compatible with other database programs like Dbase IV, FoxPro, MS
Access,Db2, MS SQL Server, Oracle, Sybase, etc.
2
10/5/2024
Advantages of SQL
6. SQL is not a case-sensitive language, i.e., both capital and small letters are recognized and
treated equally by it.
7. Powerful language: All SQL operations are performed at a prescribed and fixed level, i.e., one
SELECT command can retrieve data from multiple rows and one UPDATE command can edit
multiple rows at a time. These features make SQL a very powerful language as compared to
other languages where one command can process a single record at a time.
8. Reliable: SQL provides a well-defined set of commands that gives desirable results without
any ambiguity.
9. Freedom of data abstraction: SQL provides a greater degree of freedom of abstraction
compared to any other procedural language.
10. Complete language for a database: Apart from being a strong query-processing
language, it can also be used to create, insert, delete and control access to data in
databases
SQL Commands
SQL commands are classified into five:
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Control Language (DCL)
• Data Query Language (DQL)
• Transaction Control Language (TCL)
3
10/5/2024
Data Definition Language (DDL)
• DDL statements allows us to perform tasks related to data definition, i.e., related to the
structure of database objects.
• They are used to create, modify, and drop database objects, such as tables.
• Examples of DDL commands are:
• CREATE DATABASE
• USE DATABASE
• CREATE TABLE
• DESC
• ALTER TABLE
• DROP TABLE
Data Manipulation Language (DML)
• DML statements are used to manipulate data i.e. records or rows in a table or relation.
• It allows us to insert, update and delete data from tables.
• Examples of DML Statements are:
• INSERT INTO
• UPDATE
• DELETE
4
10/5/2024
Data Query Language (DQL)
•DQL commands helps the user to access or retrieve data.
•Only one DQL command is there:
• SELECT
Most Commonly used SQL Commands
5
10/5/2024
What is MySQL?
• MySQL is an open-source and freely available
Relational Database Management System (RDBMS) that
uses Structured Query Language (SQL)
Advantages of MySQL
1. Reliability and Performance: MySQL is a very reliable and high-performance RDBMS.
2. Modifiable: Being an open-source software, it is easily modifiable.
3. Multi-platform Support: MySQL supports several different platforms like UNIX, Linux,
macOS and Microsoft Windows.
4. Powerful Processing Capabilities: MySQL is a powerful, easy, compatible and fast and
can handle complicated corporate applications and processing requirements.
5. Integrity (Checks): MySQL provides various integrity checks in order to restrict user
input and processing.
6. Authorization: MySQL provides DDL commands to check for user authentication and
authorization by restricting access to relations and views
6
10/5/2024
MySQL Data types
• In SQL, each column of the table is assigned a data type which conveys the kind
of value that will be stored in the column.
• MySQL datatypes are classified as follows:
Numeric Data types
7
10/5/2024
Numeric Data types
Non-numeric or String Data types
8
10/5/2024
Date & Time Data types
9
10/5/2024
10
10/5/2024
11
10/5/2024
12
10/5/2024
13
10/5/2024
14
10/5/2024
15
10/5/2024
16
10/5/2024
17
10/5/2024
18
10/5/2024
19
10/5/2024
20
10/5/2024
21
10/5/2024
22
10/5/2024
23
10/5/2024
24
10/5/2024
Sorting in SQL - ORDER BY:
If you want to sort or order the records of a table , you can use the
ORDER BY clause of SQL, SELECT statement as per following format:
Syntax:-
SELECT <column names> FROM <table>
[WHERE <condition>]
ORDER BY <column name 1 > [ASC/DESC] [, <column name2>
[ASC/DESC], ...];
Keywords ASC and DESC denote the order - ASC stands for ascending
and DESC stands for descending.
25
10/5/2024
Sorting in ascending order using ORDER BY:
• If you do not specify any order keyword ASC or DESC, then by default ORDER
BY clause sorts the result set in ascending order.
For example:
SELECT * FROM student
ORDER BY marks;
OR
SELECT * FROM student
ORDER BY marks ASC;
Sorting in descending order using ORDER BY:
• To sort records in descending order
For example:
SELECT * FROM student
ORDER BY marks desc;
26
10/5/2024
Ordering Data on Multiple Columns:-
To order the result set on multiple columns, you can specify the multiple
column names in ORDER by clause along with the desired sort order, i.e.,
as:
SELECT <comma separated select list> FROM <table>
[WHERE <condition>]
ORDER BY <fieldname1> [ASC/DESC], [<field name1> [ASC|DESC], ..... ];
For example:
The following statement will sort the records firstly on the column name
and then on the basis of descending order of column marks.
SELECT * FROM student
ORDER BY name ASC, marks DESC;
Sorting on Column Alias:-
If you want, you can provide a column alias name to the
mathematical expression in the select list.
For example:
SELECT rollno, name, grade, section, marks * 0.35 AS term1 FROM
DATA
WHERE marks > 60
ORDER BY section ASC, term1 DESC;
27
10/5/2024
28
10/5/2024
29
10/5/2024
30
10/5/2024
31
10/5/2024
32