[Unit 3] Introduction to SQL
Introduction
SQL (Structured Query Language)
● SQL is a language to specify queries in structured
manner.
○ Structured means relational data.
○ SQL is a language to specify queries in a
relational database.
● SQL (Structured Query Language) is a computer
language for storing, manipulating, and retrieving
● data stored in relational databases. SQL allows
users to communicate with Relational Databases
and retrieve data from their tables
● SQL is the standard language for RDBMS.
○ All Relational Database Management Systems
(RDBMS) like "MySQL, MS Access, Oracle,
1
Sybase, DB2, Informix, postgres and SQL
Server" use SQL as standard database
language.
○ The data in RDBMS is stored in database
objects called tables.
○ A table is a collection of related data entries
and it consists of columns and rows.
● IBM implemented the SQL language, originally
called SEQUEL (Structured English Query
Languagas part of the System R project in the
early 1970s.
● SEQUEL is renamed/shortened to SQL (Structured
Query Language)
● SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in
1987.
2
● ANSI and ISO standard SQL has different
versions:SQL-86, SQL-89, SQL-92, SQL 1999,
SQL-2003, SQL 2006, SQL 2008, SQL 2011, SQL
2016
● SQL is a Domain-Specific Language.
● SQL is Declarative language i.e. a non-procedural
language.
Question: How the query is actually performed in
SQL?
● SQL is based on Relational Algebra and Tuple
Relational Calculus.
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
3
● 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
SQL Sub-languages
1.Data Definition Language (DDL)
2.Data Manipulation Language (DML)
3.Data Control Language (DCL)
4.Transaction Control Language (TCL)
Types of SQL Commands
1.Data Definition Language (DDL) commands are
used to define the structure and schema of the database.
4
● CREATE - to create new table or database
● ALTER - to alter the structure of table
● DROP - to delete a table from database
● TRUNCATE - to delete all records from table
● RENAME - to rename a table
2.Data Manipulation Language (DML) commands
are used for accessing and manipulating the data stored
in the database.
● SELECT - to retrieve data from the database
● INSERT - to insert a new row into a table
● UPDATE - to update existing row in a table
● DELETE - to delete a row from a table
● Programmers call these DML operations "CRUD".
● CRUD stands for Create, Read, Update, Delete
INSERT,SELECT, UPDATE, DELETE
5
3.Data Control Language (DCL) are the commands
to control the access of the data stored in the database
● GRANT - grant permission to user for database
access
● REVOKE - take back granted permission from
user
4.Transaction Control Language (TCL) commands
are used to manage the changes made by the DML
statement.
● COMMIT - to permanently save the transaction
● ROLLBACK - to undo transaction
● SAVEPOINT - to temporarily save a transaction so
that you can rollback to that point whenever
necessary
Note: TCL Commands are used for only DML
commands while DDL and DCL commands are auto-
commited.
SQL Rules
6
● SQL is NOT case sensitive.
For example: select is the same as SELECT
● But names of databases, tables and columns are
case sensitive.
● For example: In given SQL query, ACCOUNT is
different from account
● SELECT * FROM ACCOUNT;
SELECT FROM account;
● SQL statements can use multiple lines
○ End each SQL statement with a semi-colon;
SQL: Data Definition Language (DDL)
● Data Definition Language (DDL) is used for
creating and modifying the database objects such
as tables, indices, views and users.
● DDL Commands are used to define the structure
and schema of the database
7
● All the command of DDL are auto-committed that
means it permanently save all the changes in the
database.
● Data Definition Language (DDL) Commands:
1.CREATE - to create new table or database
2.ALTER - to alter (or modify) the structure of
table
3.DROP - to delete a table from database
4.TRUNCATE to delete all records from table
5.RENAME to rename a table
1. CREATE
● CREATE statement is used to create database
schema and to define the type and structure of the
data to be stored in the database.
● CREATE statement can be used for
○ Creating a Database,
8
○ Creating a Table, etc.
Creating a DATABASE:
CREATE DATABASE statement is used to create a
database in RDBMS.
Syntax:
CREATE DATABASE database_name;
Example:
CREATE DATABASE my_db;
Note:
SHOW statement: To see existing databases and tables
SHOW databases;
SHOW tables;
*USE statement: To use or select any existing database
Syntax
USE database_name;
Example:
9
USE my_db;
SHOW tables;
CREATE TABLE statement is used to create a new
table in a database.
It specifies column names of the table, its data types
(e.g. varchar, integer, date, etc.) and can also specify
integrity constraints (e.g. Primary key, foreign key, not
null, unique).
10
Note:
DESC (or DESCRIBE) statement: To describe the
details of the table structure
Syntax:
DESC table_name;
Example:
DESC Emp;
SQL DATA TYPES
● Each column in a database table is required to have
a name and a data type.
● 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.
11
● Note: Data types might have different names in
different database. And even if the name is the
same, the size and other details may be different!
Always check the documentation!
SQL Constraints
● SQL constraints are used to specify rules for the
data in a table.
12
● Constraints are used to limit the type of data that
can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any
violation between the constraint and the daia
action, the action is aborted.
● Constraints can be column level or table level.
○ Column level constraints apply to a column
○ Table level constraints apply to the whole
table.
● Constraints can be specified:
○ when a table is created with the CREATE
TABLE statement, or
○ we can use the ALTER TABLE statement to
create constraints even after the table is
created.
SQL Constraints:
NOT NULL: Ensures that a column cannot have
NULL value.
13
DEFAULT: Provides a default value for a column
when none is specified.
UNIQUE: Ensures that all values in a column are
different.
PRIMARY KEY: A combination of a NOT NULL
and UNIQUE Uniquely identifies each row in a table
FOREIGN KEY:Uniquely identifies a row/record in
another table.
CHECK: Ensures that all the values in a column
satisfies certain conditions
2. ALTER
14
ALTER TABLE statement is used to modify the structure of
the existing table
It is used to add, delete, modify or rename columns in an
existing table.
It is also used to add and drop various constraints on an
existing table.
ALTER TABLE - ADD COLUMN
For adding new columns in a table
ALTER TABLE - DROP COLUMN
For removing existing columns in table
ALTER TABLE - MODIFY COLUMN
To modify existing columns in a table
ALTER TABLE - RENAME COLUMN
To rename an existing column in a table
ALTER TABLE - ADD COLUMN
For adding new columns in a table
Syntax:
15
ALTER TABLE table_name
ADD column_name(s) datatype(s);
Example:
ALTER TABLE Emp
ADD Email varchar(100);
2.ALTER TABLE - DROP COLUMN
For removing existing columns in a table
Syntax:
ALTER TABLE table_name DROP column_name(s);
Example:
ALTER TABLE Emp DROP Email;
3. ALTER TABLE - MODIFY COLUMN
To modify existing columns in a table
To change data type of any column or to modify its size.
16
Syntax:
ALTER TABLE table_name MODIFY column_name
datatype;
Example:
ALTER TABLE Emp
MODIFY Name varchar(100);
3. DROP
DROP TABLE statement completely removes a table from
the database.
This command will destroy the table structure and the data
stored in it.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE Emp;
DROP command is also be used on Databases. will vibrate
17
DROP DATABASE statement is used to delete the complete
database.
Syntax:
DROP DATABASE database_name;
Example:
DROP DATABASE my_db;
4. TRUNCATE
TRUNCATE TABLE statement is used to remove all rows
(complete data) from a table. It is similar to the DELETE
statement with no WHERE clause.
TRUNCATE TABLE Vs DROP TABLE
DROP TABLE command can also be used to delete complete
table but it deletes table structure too. TRUNCATE TABLE
doesn't delete the structure of the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
18
TRUNCATE TABLE Emp;
5. RENAME
RENAME TABLE statement is used to change the name of a
table.
Syntax:
RENAME TABLE old_table_name To new_table_name;
Example:
RENAME TABLE Emp To Employee;
SQL: Data Manipulation Language (DML)
Data Manipulation Language (DML) commands are used for
accessing and manipulating the data stored in the database.
The DML commands are not auto-committed that means it
can't permanently save all the changes in the database. They
can be rollback.
Data Manipulation Language (DML)
SELECT to retrieve data from the database
INSERT - to insert a new row into a table
19
UPDATE - to update existing row in a table
DELETE - to delete a row from a table
Programmers call these DML operations "CRUD"
CRUD stands for Create, Read, Update, Delete
INSERT, SELECT, UPDATE, DELETE
1. SELECT (mostly used SQL command/querySQL
Introduction
● SELECT statement is used to select a set of data from a
database table. Or Simply SELECT statement is used to
retrieve data from a database table.
○ It returns data in the form of a result table. These
result tables are called result-sets.
● SELECT is also called DQL because it is used to query
information from a database table
● SELECT statement specifies column names, and FROM
specifies table name
● SELECT command is used with different Conditions
and CLAUSES.
20
WHERE CLAUSE
WHERE Clause is used to select a particular record based on
a condition. It is used to filter records.
WHERE Clause is used to specify a condition while fetching
the data from a single table or by joining with multiple
conditions
21
The WHERE clause is not only used in the SELECT
statement, but it is also used in the UPDATE, DELETE
statement.
"SELECT with WHERE Clause"
Syntax:
SELECT column 1, column2, ... columnN
FROM table_name(s)
WHERE condition;
Basic Query Structure
SQL is based on set and relational operations with certain
modifications and enhancements
A typical SQL query has the form:
select A1, A2,……An
from r₁,r2,.............rn
where P
A represent attributes
22
r represent relations
P is a predicate (or condition)
Optional Clauses in SELECT statement
● [WHERE Clause]: It specifies which rows to retrieve by
specifying conditions.
● [GROUP BY Clause]: Groups rows that share a
property so that the aggregate function can be applied to
each group.
● [HAVING Clause]: It selects among the groups defined
by the GROUP BY clause by specifying conditions..
● [ORDERBY]: It specifies an order in which to return
the rows.
● [DISTINCT Clause]: It is used to remove duplicates
from results set of a SELECT statement. (SELECT
DISTINCT)
SQL Operators
1. SQL Arithmetic Operator
2. SQL Comparison Operators
23
3. SQL Logical Operators
4. SQL Special Operators
1.Arithmetic Operators are:
+ [Addition]
- [Subtraction]
/ [Division]
* [Multiplication]
% [Modulus]
Example
To retrieve employee_id, employee_name, salary plus 100
From emp table.
SELECT employee_id, employee_name, salary + 100
FROM emp ;
2.Comparison Operators are
24
Example:
To retrieve Emp_ID, Name from Emp table whose Salary is
greater than 2000
SELECT Emp_ID, Name, Salary FROM Emp
WHERE Salary>2000;
3. SQL Logical Operators
AND- It displays a record if all the conditions separated by
AND are TRUE.
OR- It displays a record if any of the conditions separated by
OR is TRUE
NOT- It displays a record if the condition(s) is NOT TRUE.
it reverses the meaning of any operator with which it is used.
This is a negate operator. Eg: NOT EXISTS, NOT
BETWEEN, NOT IN, IS NOT NULL, etc.
Example:
To retrieve all acords from Emp table whose salary is greater
than 2000 and age is less than 25
25
SELECT
FROM Emp
WHERE Salary>2000 AND Age<25;
4.Special Operators are:
2. INSERT
26
The INSERT INTO statement is used to insert new records in
a table.
It is possible to write the INSERT INTO statement in two
ways.
Syntax 1: Specify both the column names and the values to
be inserted
INSERT INTO table_name(column1, column2,
column3,...columnN) VALUES (value1, value2,
value3,...valueN);
Syntax 2: Specify only values to be inserted. But needed to
remember the column order
INSERT INTO table_name VALUES (value1, value 2, value
3,...valueN);
27
3. UPDATE
The UPDATE statement is used to modify the existing
records in a table.
Note: Always use the WHERE clause with the UPDATE
statement to update the selected rows, otherwise all the rows
would be affected.
Syntax
UPDATE table_name SET column1 = value1, column2 =
value 2,............. columnN=valueN WHERE condition;
Example: UPDATE
Example: To update the address of Emp_ID: 3 to Chennai
UPDATE Emp
SET Address='Chennai'
WHERE Emp_ID=3;
28
To Check:
SELECT FROM Emp;
4. DELETE
The DELETE statement is used to delete existing records in
a table.
Note: Always use the WHERE clause with a DELETE
statement to delete the selected rows, otherwise all the
records would be deleted.
Syntax:
DELETE FROM table_name WHERE condition;
Example:To delete the employee records of Pune location
DELETE FROM Emp
WHERE Address='Pune';
29