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

Relational Database 2

Uploaded by

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

Relational Database 2

Uploaded by

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

Structured

Query
Language
Database changed
mysql> select * from company;
+------+-------+
| SID | SALES |
+------+-------+
| S101 | 20000 |
| S103 | NULL |
| S104 | 10000 |
| S105 | 15000 |
+------+-------+
4 rows in set (0.31 sec)

mysql> select avg(sales) from company;


+------------+
| avg(sales) |
+------------+
| 15000.0000 |
+------------+
1 row in set (0.07 sec)
Objectives:
Students should be able to understand :
1. SQL
2. Statements provided by SQL for a variety of
tasks
3. Advantages of using SQL
4. Types of SQL Statements
5. Data Definition Language (DDL) Statements
6. Data Manipulation Language (DML) Statements
7. Data Types
Structured Query Language (SQL)

Structured Query Language (SQL) is a standard


language used for accessing databases.

This is a special purpose programming language


used to create a table, manage data and
manipulate data.
SQL provides statements for a variety of tasks,
including:

i) Querying data
ii) Inserting, updating, and deleting rows in a table
iii) Creating, replacing, altering, and dropping
objects (tables)
iv) Controlling access to the database and its objects
(tables)
v) Guaranteeing database consistency and integrity
SQL unifies all of the preceding tasks in one
consistent language.
Advantages of using SQL:
1) SQL is portable: SQL is running in all servers, mainframes,
PCs, laptops, and even mobile phones.
2) High speed: SQL queries can be used to retrieve large
amounts of records from a database quickly and efficiently.
3) Easy to learn and understand: SQL generally consists of
English statements and as such, it is very easy to learn and
understand. Besides, it does not require much coding unlike
in programming languages.
4) SQL is used with any DBMS system with any vendor: SQL
is used by all the vendors who develop DBMS. It is also used
to create databases, manage security for a database, etc. It
can also be used for updating, retrieving and sharing data
with users.
Advantages of using SQL:
5) SQL is used for relational databases: SQL is widely used
for relational databases.
6) SQL acts as both programming language and interactive
language: SQL can do both the jobs of being a programming
language as well as an interactive language at the same time.
7) Client/Server language: SQL is used for linking front end
computers and back end databases. It provides client server
architecture (Email, and the World Wide Web - all apply the
client-server architecture).
8) Supports object based programming: SQL supports the
latest object based programming and is highly flexible.
Types of SQL Statements
The SQL statements are categorized into different
categories based upon the purpose. They are;
i) Data Definition Language (DDL) statement
ii) Data Manipulation Language (DML) statement
iii) Transaction Control Statement
iv) Session Control Statement
v) System Control Statement
vi) Embedded SQL Statement

Out of these six, we will be studying only the first two


types in this course. (as per CBSE Syllabus)
Data Definition Language (DDL)
Statements
Data Definition Language (DDL) or Data Description
Language (DDL) is a standard for commands that
defines the different structures in a database. DDL
statements are used to create structure of a table,
modify the existing structure of the table and
remove the existing table.
Some of the DDL statements are CREATE TABLE,
ALTER TABLE and DROP TABLE.
Data Manipulation Language (DML)
Statements
Data Manipulation Language (DML) statements are
used to access and manipulate data in existing
tables.
The manipulation includes inserting data into
tables, deleting data from the tables, retrieving
data and modifying the existing data.
The common DML statements are SELECT, UPDATE,
DELETE and INSERT.
Data Types
Each value manipulated by SQL Database has a data
type. The data type of a value associates a fixed set
of properties with the value.
Data Types: number / decimal, char / varchar /
varchar2, date; (as per CBSE Syllabus)
In SQL there are three main data types:
1. Character
2. Number and
3. Date types.
Character
Character data types stores character (alphanumeric)
data, which are words and free-form text. They are less
restrictive than other data types and consequently have
fewer properties. For example, character columns can
store all alphanumeric values, but number columns can
store only numeric values.

Character data types are;


i) CHAR
ii) VARCHAR
iii) VARCHAR2
CHAR:
• CHAR should be used for storing fix length character
strings. When CHAR values are stored, they are right-
padded with spaces to the specified length. The length
can be any value from 0 to 255. When CHAR values are
retrieved, trailing spaces are removed. If this type is
used to store variable length strings, it will waste a lot
of disk space (always allocate fixed memory) . If we
declare data type as CHAR, then it will occupy space
for NULL values.
• Format: CHAR(n)
• Fixed-length character string having maximum length
n.
VARCHAR:
• Varchar is a variable character string. VARCHAR
values are not padded when they are stored.
Trailing spaces are retained when values are
stored and retrieved, in conformance with
standard SQL. The length can be specified as a
value from 0 to 65,535 bytes.
• Format: VARCHAR (n)
• Variable-length character string having
maximum length n.
The following table illustrates the differences between CHAR and
VARCHAR by showing the result of storing various string values into
CHAR(4) and VARCHAR(4) columns (assuming that the column uses a
single-byte character set such as latin1).

The values shown as stored in the last row of the table apply only
when not using strict SQL mode; if strict mode is enabled, values
that exceed the column length are not stored, and an error results.
VARCHAR2:
(Used in Oracle SQL)
• VARCHAR2 is used to store variable length
character strings. The string value's length will be
stored on disk with the value itself. VARCHAR2 can
store up to 4000 bytes of characters. Thus, the
difference between VARCHAR and VARCHAR2 is
that VARCHAR is ANSI standard but takes up space
for variables, whereas the VARCHAR2 is used only
in Oracle but makes more efficient use of space.
• Format: VARCHAR2 (n)
Examples:
• CHAR(10) has fixed length, right padded with spaces.
• VARCHAR(10) has fixed length, right padded with NULL
• VARCHAR2(10) has variable length.
• Name char (10): Suppose if we store Name is as "Ravi",
then first four places of the ten characters are filled with
Ravi and the remaining 6 spaces are also allocated to
Name. Thus, the size of name is always ten.
• Name varchar (10): Suppose if we store Name is as "Ravi",
then first four places of the ten characters are filled with
Ravi and the remaining 6 spaces are filled with NULL.
• Name varchar2 (10): Suppose if we store Name is as "Ravi",
then only first four places are filled with Ravi.
The following table gives possible string data
types used in different DBMS
Numeric data type:
• Numeric data types are mainly used to store
number with or without fraction part.

• The numeric data types are:

1. NUMBER
2. DECIMAL
3. NUMERIC
4. INT
5. FLOAT
NUMBER:
The Number data type stores fixed and floating-point
numbers. The Number data type is used to store integers
(negative, positive, floating) of up to 38 digits of precision.

Format: NUMBER (p, s)


Where:
• 'p' is the precision or the total number of significant
decimal digits, where the most significant digit is the left-
most nonzero digit and the least significant digit is the
right-most known digit.
• 's' is the scale or the number of digits from the decimal
point to the least significant digit.
DECIMAL and NUMERIC:
Decimal and numeric data types have fixed
precision and scale.
Format: DECIMAL[ (p[, s] )] and NUMERIC[ (p[, s] )]
Square brackets ([ ]) are optional.

INT/INTEGER: The int data type is the integer data


type in SQL. This is used to store integer number
(without any fraction part).
FLOAT: This data type is used to store number with
fraction part(real numbers).
The following table gives possible numeric data
types used in difference DBMS
mysql> create table sample(sno int, marks float, salary decimal(10,2), profit
numeric(8,1));
Query OK, 0 rows affected (0.15 sec)

mysql> insert into sample values(1, 90.25, 1234567890.55, 12345678.2) ;


ERROR 1264 (22003): Out of range value for column 'salary' at row 1

mysql> insert into sample values(1, 90.25, 12345678.55, 12345678.2);


ERROR 1264 (22003): Out of range value for column 'profit' at row 1

mysql> insert into sample values(1, 90.25, 12345678.55, 1234567.2);


Query OK, 1 row affected (0.06 sec)

mysql> select * from sample;


+-------+---------+-------------------+--------------+
| sno | marks | salary | profit |
+-------+---------+-------------------+---------------+
| 1 | 90.25 | 12345678.55 | 1234567.2 |
+-------+---------+-------------------+---------------+
1 row in set (0.00 sec)
DATE
Date is used to store valid date values, which is
ranging from January 1, 4712 BC to December 31,
9999 AD.

The date formats are: YYYY-MM-DD or


DD/MON/YY or YYYY/MM/DD or MM/DD/YY or
DD-MON-YYYY.

• Format: DATE
Q: Answer the following questions:
1. Write two types of statements used in SQL.
2. Write two examples for each statement given in
question 1.
3. Write any four advantages of SQL.
4. Write four tasks which can be performed using SQL.
5. What are three main data types used in SQL? Write
format for each data type.
Q: Answer the following questions:
1. Write two main statements used in SQL. (DDL & DML)
2. Write two examples for each statement given in
question 1. (Some of the DDL statements are CREATE
TABLE, ALTER TABLE and DROP TABLE & The common
DML statements are SELECT, UPDATE, DELETE and
INSERT.)
3. Write any four advantages of SQL.
4. Write four tasks which can be performed using SQL.
5. What are three main data types used in SQL? Write
format for each data type. (In SQL there are three main
data types: Character, Number and Date types.)
Q: Which data type you will use for the following
attributes/fields/records in table Hospital
TABLE : HOSPITAL
No Name Age Department Datofadm Charges Gender
1 Sandeep 65 Surgery 23/02/98 300 M
2 Ravina 24 Orthopedic 20/01/98 200 F
3 Karan 45 Orthopedic 19/02/98 200 M
4 Tarun 12 Surgery 01/01/98 300 M
5 Zubin 36 ENT 12/02/98 250 M
6 Ketaki 16 ENT 24/02/98 300 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shaliya 31 Nuclear 19/02/98 400 M
Medicine
Write a statement to create the table Hospital
mysql> CREATE TABLE HOSPITAL (No INT(3), Name VARCHAR(10),
Age INT(3), Department VARCHAR(15), DateOfAdm DATE, Charges
DECIMAL(7,2),Gender CHAR(1));
Query OK, 0 rows affected, 2 warnings (0.56 sec)
Q: Which data type you will use for the following
attributes/fields/records in Table: STOCK
Table: STOCK
ItemNo Item Dcode Qty UnitPrice StockDate
5005 Ball Pen 0.5 102 100 16.00 31-Mar-10
5003 Ball Pen 0.25 102 150 20.00 01-Jan-10
5002 Gel Pen Premium 101 125 14.00 14-Feb-10
5006 Gel Pen Classic 101 200 22.50 01-Jan-09
5001 Eraser Small 102 210 5.75 19-Mar-09
5004 Eraser Big 102 60 10.50 12-Dec-09
5009 Sharpener Classic 103 160 8.00 23-Jan-09

Write a statement to create the table STOCK.


Q: Which data type you will use for the following
attributes/fields/records in Table: EMP
Structure of the EMP Table
Q: Which data type you will use for the following
attributes/fields/records in Table: DEPT
1st Create Database
2nd. Use Database
Command to create table and display its structure
(7698,

NU
Structure of the DEPT Table

Q. Write a statement to make DEPTNO


Primary Key in DEPT table.
Statement to make DEPTNO Primary key.

mysql> alter table dept add primary key(deptno);


Explain
what is
happening
here?
Questions Based on Emp and Dept tables.
Q1 Display all records from table EMP.
Q2. Display empno and ename of all employees from table emp
mysql> select empno, ename from emp;
Q3. Display ename, sal, sal added with comm from emp.
SELECT ENAME, SAL, SAL+COMM FROM EMP;
Q4. Display ename joined with job with heading "Employee", sal*12 as "Total Salary" from table emp.
SELECT CONCAT(ENAME,' ', JOB) "EMPLOYEE", SAL* 12 "TOTAL SALARY" FROM EMP;
Q5. Display distinct sal of employees from table emp.
SELECT DISTINCT SAL FROM EMP;
Q6. Show the structure of table Dept.
desc emp;
Q7. Write a Query to display Ename and sal of employees whose salary is greater than or equal to 3000 from
table emp.
SELECT ENAME, SAL FROM EMP WHERE SAL>=3000;
Q8 Write a query to display employee name, salary and department number who are not getting commission
from table emp.
SELECT ENAME, SAL, DEPTNO FROM EMP WHERE COMM IS NULL;
Q9. Write a query to display employee Number, name , sal, sal*12 as annual Salary whose commission is not
null from table emp.
SELECT ENAME, SAL, SAL* 12 "ANNUAL SALARY" FROM EMP WHERE COMM IS NOT NULL;
Q10. Write a query to display employee name and salary of those employees who do not have their salary in
the range of 1500 to 2000.
SELECT ENAME, SAL FROM EMP WHERE SAL NOT BETWEEN 1500 AND 2000;
Q11. Write a query to display name, job, salary and hiredate of employees who are hired between February 20 1981 and May 1
1981. Order the query in ascending order of HireDate.
mysql> # Q11. Write a query to display name, job, salary and hiredate of employees who are hired between February 20 1981
and May 1 1981. Order the query in ascending order of HireDate.
mysql> SELECT ENAME, JOB, SAL, HIREDATE FROM EMP WHERE HIREDATE BETWEEN '1981-02-20' AND '1981-05-01' ORDER BY
HIREDATE;
Q12. Write a query to display the name and hiredate of all the employees who were hired in 1987.
mysql> # Q12. Write a query to display the name and hiredate of all the employees who were hired in 1987.
mysql> SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE LIKE '1987%';
mysql> SELECT ENAME, HIREDATE FROM EMP WHERE YEAR(HIREDATE)= '1987';
mysql> SELECT ENAME, HIREDATE FROM EMP WHERE YEAR(HIREDATE)= 1987;
Q13 Write a query to display the name, job title and salary of those employees who do not have manager.
mysql> # Q13 Write a query to display the name, job title and salary of those employees who do not have manager.
mysql> SELECT ENAME, JOB, SAL FROM EMP WHERE MGR IS NULL;
Q14.Write a query to display the name of employee whose name contains 'A' as third alphabet.
mysql> # Q14.Write a query to display the name of employee whose name contains 'A' as third alphabet.
mysql> SELECT ENAME FROM EMP WHERE ENAME LIKE '__A%';
Q15. Write a query to display the name of the employee whose name contains 'T' as the last alphabet.
mysql> SELECT ENAME FROM EMP WHERE ENAME LIKE '%T';
Q16. Write a Query to display the name of the employee whose name contains 'M' as first alphabet and 'L' as third alphabet.
mysql> SELECT ENAME FROM EMP WHERE ENAME LIKE 'M_L%';
Q17. Write a Query to display the name of the employee who is having 'L' as any alphabet of the name.
SELECT ENAME FROM EMP WHERE ENAME LIKE '%L%';
Q18. WAQ to display the current system date.
mysql> select sysdate();
Q19. WAQ to display employee name, salary, salary increased by 15% expressed as a whole number. Label the column as 'New
Salary'.
mysql> SELECT ENAME, SAL, ROUND(SAL+SAL*0.15) NEW_SALARY FROM EMP;
mysql> SELECT ENAME, SAL, ROUND(SAL+SAL*0.15) "NEW SALARY" FROM EMP;
mysql> SELECT ENAME, SAL, ROUND(SAL+SAL*0.15) AS NEW_SALARY FROM EMP;
Q20.WAQ to display ename, salary review date which isthe date after six months of hiredate.
Q21.WAQ to display ename, salary review date which is the date after six months of hiredate in format of
'Sunday, 7 SEP, 1981'.
Q22. WAQ to display ename and total no of weeks lapsed between hiredate and today.
Q23. WAQ to display ename and total no of days lapsed between hiredate and today.
Q24. Create a query that produces display in the following format:
<employee name> EARNS$<salary> Monthly and Working as <Job>
mysql> SELECT CONCAT(ENAME,' EARNS $ ',SAL,' MONTHLY AND WORKING AS ',JOB) FROM EMP;
Q25. WAq to display ename with first letter capitalized and other letter lowercase and lenght of their name
string.
mysql> select concat(upper(mid(ename,1,1)), lower(mid(ename,2,length(ename)))), length(ename) from
emp;
Q26. WAQ to display the ename and commission amount . If the employee does not earn commission, put 'No
commission'.
Q28. WQA to display the ename, deptno, dname for all employees using tables emp and dept.
mysql> SELECT ENAME, E.DEPTNO, DNAME FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO;
mysql> SELECT ENAME, EMP.DEPTNO, DNAME FROM EMP, DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO;
mysql> SELECT E.ENAME, E.DEPTNO, D.DNAME FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO;
mysql> SELECT EMP.ENAME, EMP.DEPTNO, DEPT.DNAME FROM EMP , DEPT WHERE
EMP.DEPTNO=DEPT.DEPTNO;
Q29. WAQ to display ename, dname and location of all employees who have manager number between 7500
and 7900.
mysql> SELECT ENAME, DNAME, LOC FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO AND MGR
BETWEEN 7500 AND 7900;
mysql> SELECT ENAME, DNAME, LOC FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO AND MGR >= 7500
AND MGR<=7900;
Q30. WAQ to display the ename, dname of all the employees that work in the same dept as the given
employee SMITH
mysql> SELECT ENAME, DNAME FROM EMP E, DEPT D WHERE E.DEPTNO=D.DEPTNO AND
E.DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='SMITH');
Q31. WAQ to display employee name and hiredate of employees who are employed after employee
'BLAKE'
mysql> SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE>(SELECT HIREDATE FROM EMP WHERE
ENAME='BLAKE');
Q32. WAQ to display the empno, name, job and manager’s name, job with their manager numbers.
SELECT E.EMPNO, E.ENAME , E.JOB, M.EMPNO, M.ENAME, M.JOB FROM EMP E, EMP M WHERE
E.MGR=M.EMPNO;
Q33. WAQ to display the sum, average , highest and lowest salary of the employees.
Q34. WAQ to display the sum, average , highest and lowest salary of the employees
grouped by deptno.
Q35. WAQ to display the sum, average , highest and lowest salary of the employees grouped by deptno
and sub grouped by job.
Q36. WAQ to display number of employees with same job.
Q37 WAQ to display average of highest and lowest salary of each department.
Q38. WAQ to display the difference of highest and lowest salary of each dept having maximum salary >
1000
Q39. WAq to display the ename and job for employees in the same dept as 'ALLEN'
Q40. WAQ to display employee name and salary of those who either work in department 10 or have salary
greater than employee 7521.
41. WAQ to display the name of the employees hired on same date.
42. WAQ to display the department numbers, and department wise total salary of the departments where
department salary > 9000.
Q31. WAQ to display employee name and hiredate of employees who are employed after employee 'BLAKE'
mysql> SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE>(SELECT HIREDATE FROM EMP WHERE ENAME='BLAKE');
Q32. WAQ to display the empno, name, job and manager’s name, job with their manager numbers.
SELECT E.EMPNO, E.ENAME , E.JOB, M.EMPNO, M.ENAME, M.JOB FROM EMP E, EMP M WHERE E.MGR=M.EMPNO;
Q33. WAQ to display the sum, average , highest and lowest salary of the employees.
mysql> SELECT SUM(SAL), AVG(SAL), MAX(SAL), MIN(SAL) FROM EMP;
Q34. WAQ to display the sum, average , highest and lowest salary of the employees
grouped by deptno.
mysql> SELECT SUM(SAL), AVG(SAL), MAX(SAL), MIN(SAL) FROM EMP GROUP BY DEPTNO;
Q35. WAQ to display the sum, average , highest and lowest salary of the employees grouped by deptno and sub grouped by job.
Q36. WAQ to display number of employees with same job.
mysql> SELECT JOB, COUNT(*) FROM EMP GROUP BY JOB;
mysql> SELECT JOB, COUNT(JOB) FROM EMP GROUP BY JOB;
Q37 WAQ to display average of highest and lowest salary of each department.
mysql> SELECT (MAX(SAL)+MIN(SAL))/2 AVERAGE FROM EMP;
Q38. WAQ to display the difference of highest and lowest salary of each dept having maximum salary > 1000
mysql> SELECT MAX(SAL)-MIN(SAL) DIFFERENCE, DEPTNO FROM EMP GROUP BY DEPTNO HAVING MAX(SAL)>1000;
Q39. WAQ to display the ename and job for employees in the same dept as 'ALLEN‘
mysql> SELECT ENAME, JOB FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME ='ALLEN');
Q40. WAQ to display employee name and salary of those who either work in department 10 or have salary greater than
employee 7521.
mysql> SELECT ENAME, SAL FROM EMP WHERE DEPTNO=10 OR SAL >(SELECT SAL FROM EMP WHERE EMPNO=7521);
41. WAQ to display the name, hiredate of the employees hired on same date.
mysql> SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE = (SELECT HIREDATE FROM EMP GROUP BY HIREDATE HAVING
COUNT(*)>1);
42. WAQ to display the department numbers, and department wise total salary of the departments where SUM OF department
salary > 9000.
mysql> SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY DEPTNO HAVING SUM(SAL)>9000;
SQL commands are used to implement the
following:
• SQL can retrieve data from a database
• SQL can insert records in a database
• 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 views in a database
CREATE TABLE Command
CREATE TABLE command is used to create table structure. In this
command, we need to give full information about table such as number
of columns, type of each column and constraints (primary key).
The CREATE TABLE command requires:
• Name of the table,
• Names of fields,
• Definitions and constrains for each field.

Constraints
In SQL, we have the following constraints:
• NOT NULL - To check a column cannot store NULL value.
• PRIMARY KEY - To check that a column has an unique identity which
helps to find a particular record in a table. It can’t have NULL values
• Unique: To check that a column has unique values. It can have NULL
values.
https://www.w3schools.com/mysql/mysql_constraints.asp
CREATE TABLE Syntax:
CREATE TABLE<table name>
(<column name1> <data type> [size] [ constraints
],
<column name2> <data type>[size] [constraints],
.
.
.
<column name n> <data type>[size] [constraints]);
Example: Create the following Table Classlist
Column Name Data Type Size Constraints
ROLL int 2 PRI
NAME varchar 15
DOB date
SUB1 iNT 3
SUB12 INT 3
SUB13 INT 3
AVG Decimal 5,2
MOBILE varchar 10
EMIRATES varchar 10
Command to Create Table CLASSLIST

CREATE TABLE CLASSLIST(


ROLL INT(2) PRIMARY KEY,
NAME VARCHAR(15),
DOB DATE,
SUB1 INT(3), SUB2 INT(3), SUB3 INT(3),
AVG DECIMAL(5,2),
MOBILE VARCHAR(10),
EMIRATES VARCHAR(10));
INSERT INTO Command:
This command is used to add rows in the table, but
can add only one row at a time.
Syntax:
INSERT INTO <table name> [Column_name1,
Column_name2, ......Column_name n] VALUES
(value1,value2,value3,….,value n);
OR
INSERT INTO <table name> VALUES (value1, value2,
value3, …., value n);
Note: [] Optional
Example: Inserting new rows/tuples/records in Table
CLASSLIST

Roll Name DOB Sub1 Sub2 Sub3 Avg Mobile Emirates

1 AADITYA 1999-12-19 65 75 65 NULL 0504681433 DUBAI

2 ABEL 1999-05-19 99 90 98 NULL 0552660331 DUBAI

3 VIVEK 1999-06-02 70 80 76 NULL 0563081362 DUBAI

4 TANAY 2000-03-23 90 90 90 NULL 0556474152 DUBAI

5 ROBIN 1999-08-23 85 87 89 null 0567017497 SHARJAH


Example: Inserting new rows/tuples/records in Table CLASSLIST

INSERT INTO CLASSLIST VALUES(1,'AADITYA','1999-12-


19',65,75,65,NULL, '0504681433', 'DUBAI');

INSERT INTO CLASSLIST VALUES (2,'ABEL', '1999-05-19',99,90,98,null,


'0552660331', 'DUBAI');

INSERT INTO CLASSLIST VALUES(3,"VIVEK",'1999-06-02',70,80, 76,NULL,


'0563081362', 'DUBAI');

INSERT INTO CLASSLIST VALUES(4,"TANAY","2000-03-23",90,90,90,null,


"0556474152", "DUBAI");

insert into classlist values(5,"ROBIN","1999-08-23",85,87,89,null,


"0567017497", "SHARJAH");
SELECT Command
This command is used to view table information from SQL
database. By using SELECT command, we can get one or more
fields information, while using *, one can get all fields
information.
Syntax:
SELECT [* | DISTINCT ]
[FROM table_references
[WHERE where_condition]
[GROUP BY col_name ]
[HAVING having_condition]
[ORDER BY {col_name }
[ASC | DESC]]
We can specify any condition using where clause. Where clause
is optional.
Write queries for the following questions:

1. Display CLASSLIST table.

2. To display name and dob of classlist table.

3. To display name of students staying in DUBAI from


classlist table.
Answers:
1. Display CLASSLIST table.
SELECT * FROM classlist;
This will display all information of the particular table
(classlist)
2. To display name and class of classlist table.
SELECT name, dob FROM classlist;
3. To display name of students staying in DUBAI from
classlist table.
SELECT name, emirates FROM classlist WHERE
emirates =‘DUBAI’;
Operators used in SQL commands:

Arithmetic operators:
Arithmetic operator takes two operands and
performs a mathematical calculation on them.
However, they can be used only in SELECT
command. The arithmetic operators used in SQL
are:
+ Addition
- Subtraction
* Multiplication
/ Division
Write query to add sub1, sub2 and sub3 marks and
displaying it under heading Total
Adding sub1, sub2 and sub3 marks and displaying it as Total

SELECT sub1+sub2+sub3 AS Total FROM classlist;


SELECT sub1+sub2+sub3 Total FROM classlist;
SELECT sub1+sub2+sub3 "Total" FROM classlist;
SELECT sub1+sub2+sub3 as "Total" FROM classlist;
+-------+
| Total |
+-------+
| 205 |
| 287 |
| 226 |
| 270 |
| 261 |
+-------+
Relational operators in SQL queries:
Relational operators are used to implement
comparison between two operands. These
operators can be used only in 'where clause'.
Relational operators are -
< less than
> greater than
<= less than or equal to
>= greater than or equal to
= equal to
!= not equal to
Write SQL statement to compute average marks of
each student and store it in column avg.
(Single statement should fill all values in avg column.)
Q. Write the queries to :
1. Display names of students having Avg>=90.
2. Display name of students having Avg<90
Roll Name DOB Sub1 Sub2 Sub3 Avg Mobile Emirates

1 AADITYA 1999-12-19 65 75 65 68.33 0504681433 DUBAI

2 ABEL 1999-05-19 99 90 98 95.67 0552660331 DUBAI

3 VIVEK 1999-06-02 70 80 76 75.33 0563081362 DUBAI

4 TANAY 2000-03-23 90 90 90 90.00 0556474152 DUBAI

5 ROBIN 1999-08-23 85 87 89 87.00 0567017497 SHARJAH


1. Display names of 2. Display name of
students having students having Avg<90
Avg>=90.
Ans.:
Ans.:
SELECT name FROM SELECT name FROM
classlist WHERE classlist WHERE avg<90;
avg>=90;
3. Display names of 4. Display name of
students NOT staying in students who are born
DUBAI. after 1st January 2000.
SELECT NAME FROM
SELECT NAME FROM CLASSLIST WHERE
CLASSLIST WHERE DOB>'2000-01-01';
EMIRATES != 'DUBAI'; +-----------+
+-----------+
| NAME | | NAME |
+-----------+ +-----------+
| ROBIN | | TANAY |
+-----------+ +----------+
Logical operators in SQL queries :

Logical operators are also possible only in 'where


clause' and are used to merge more than one
condition.

Logical operators are:


• AND
• OR
• NOT
1. Display names of 2. Display name of
students staying in students who are having
DUBAI and having AVG between 80 & 90.
AVG>90.
1. Display names of 2. Display name of
students staying in students who are having
DUBAI and having AVG between 80 & 90.
AVG>90. SELECT NAME FROM
SELECT NAME FROM CLASSLIST WHERE
CLASSLIST WHERE AVG>=80 AND AVG<=90;
EMIRATES = 'DUBAI' +-----------+
AND AVG>90;
+----------+ | NAME |
| NAME | +-----------+
+----------+ | TANAY |
| ABEL | | ROBIN |
+----------+ +-----------+
LIKE Operator in SQL Queries
• LIKE OPERATOR is used to search a value similar
to specific pattern in a column using wildcard
operator.
• There are two wildcard operators - percentage
sign (%) and underscore ( _ ).
• The percentage sign represents zero, one, or
multiple characters, while the underscore
represents a single number or character.
• The symbols can be used in combinations.
Write a query to display names having only 5
characters.
1. Write a query to display names having only 5 characters.

SELECT NAME FROM CLASSLIST WHERE NAME LIKE '_____';


OR
SELECT NAME FROM CLASSLIST WHERE LENGTH(NAME)=5;
+----------+
| NAME |
+----------+
| VIVEK |
| TANAY |
| ROBIN |
+---------+
2. Display the names 3. Display the names
that start with letter whose name's 3rd letter
"A". is ‘V'.
2. Display the names 3. Display the names
that start with letter whose name's 3rd letter
"A". is ‘V'.
SELECT NAME FROM
SELECT NAME FROM CLASSLIST WHERE NAME
CLASSLIST WHERE LIKE '__V%';
NAME LIKE 'A%'; +-----------+
+--------------+
| NAME | | NAME |
+--------------+ +-----------+
| AADITYA | | VIVEK |
| ABEL | +-----------+
+--------------+
IN Operator in SQL queries
The IN operator allows us to specify multiple
values in a WHERE clause.

Write a query to Display students' name, who are


in SHARJAH or DUBAI. (using IN operator)

Write a query to Display students' name, who are


in SHARJAH or DUBAI. (Without using IN operator)
Write a query to Display students' name, who are in SHARJAH or DUBAI.
(using IN operator)

Write a query to Display students' name, who are in SHARJAH or DUBAI.


(Without using IN operator

select name from classlist where emirates in ('dubai', 'sharjah');

select name from classlist where emirates='dubai'or emirates= 'sharjah';


+--------------+
| name |
+--------------+
| AADITYA |
| ABEL |
| VIVEK |
| TANAY |
| ROBIN |
+--------------+
BETWEEN Operator in SQL Queries
The BETWEEN operator is used to test whether or not a
value (stated before the keyword BETWEEN) is
"between" the two values stated after the keyword
BETWEEN.

Write query to display students' name, who are having


Avg between 80 and 90. (Using Between operator)
Write query to display students' name, who are having
Avg between 80 and 90. (Without Using Between
operator)
Write query to display students' name, who are having Avg
between 80 and 90. (Using Between operator)
Write query to display students' name, who are having Avg
between 80 and 90. (Without Using Between operator)
SELECT NAME FROM CLASSLIST WHERE AVG BETWEEN 80 AND
90;
SELECT NAME FROM CLASSLIST WHERE AVG>=80 AND
avg<=90;
+-----------+
| NAME |
+-----------+
| TANAY |
| ROBIN |
+-----------+
[Note: In the above Query 80 and 90 is also included]
ORDER BY Command in SQL Query

This command is used to arrange values in


ascending or descending order.
Write query for the following questions:
1. Display student names in ascending order.
2. Display student names in descending order.
Display student names in Display student names in
ascending order. descending order.
select name from classlist
select name from classlist order by name desc;
order by name asc;
+-----------+
+-------------+
| name |
| name |
+------------+
+-------------+
| VIVEK |
| AADITYA |
| TANAY |
| ABEL |
| ROBIN |
| ROBIN |
| ABEL |
| TANAY |
| AADITYA |
| VIVEK |
+-------------+
+-------------+
'asc' for ascending order. Without asc also the list is
displayed in ascending order only.
Aggregate functions in SQL Queries

Aggregate functions are used to implement


calculation based upon a particular column. These
functions always return a single value.

Aggregate functions are:


1. SUM()
2. AVG()
3. MAX()
4. MIN()
5. COUNT()
SUM()
This function is used to find the total value of a
particular column.
Q: Write a query to find total of each subject in the
table classlist.
Q: Write a query to find total of each subject in the
table classlist
SELECT SUM(SUB1), SUM(SUB2), SUM(SUB3) FROM
CLASSLIST;
+-----------------+-----------------+----------------+
| SUM(SUB1) | SUM(SUB2) | SUM(SUB3) |
+-----------------+-----------------+-----------------+
| 409 | 422 | 418 |
+-----------------+-----------------+-----------------+
AVG()
This function is used to find the average value of a
particular column.

Q: Write a query to find average of each subject


from tables classlist.
Q: Write a query to find average of each subject
from tables classlist.
MAX()
This function is used to find the maximum value of a
particular column.
Q: Write a query to display highest mark in each
subject.
Q: Write a query to display highest mark in each
subject.
MIN()
This function is used to find the minimum value of
a particular column.
Q: Write a query to display lowest mark in each
subject.
Q: Write a query to display lowest mark in each
subject.
COUNT()
This function is used to find the number of
values (i.e. number of rows) of a particular
column.
Q: Write a query to count the number of
students in table classlist.
COUNT()
Q: Write a query to count the number of
students in table classlist.
GROUP BY
The SQL GROUP BY is a clause that enables SQL
aggregate functions for grouping of information.
(i.e. GROUP BY clause is used in collaboration with
the SELECT statement to arrange identical data into
groups.). This clause is used whenever aggregate
functions by group are required.

Q: Display emirates and number of students from


each emirates.
GROUP BY
Q: Display emirates and number of students from
each emirates.
Having clause
As mentioned earlier, the 'where' clause is used only
to place condition on the selected columns,
whereas the 'HAVING' clause is used to place
condition on groups created by 'group by' clause,
because here the 'WHERE' clause is not useable.

Q: Write a query to display emirates, number of


the students where number of students in more
than 1.
Having clause
Q: Write a query to display emirates, number of
the students where number of students in more
than 1.
DISTINCT
The DISTINCT keyword is used to remove
duplicate values in a particular column.

Q1. Write a query to display all Emirates from


classlist.

Q2. Write a query to display unique Emirates


from classlist.
DISTINCT
SELECT EMIRATES FROM CLASSLIST;
+---------------+
| EMIRATES |
+--------------+
| DUBAI |
| DUBAI |
| DUBAI |
| DUBAI |
| SHARJAH |
+--------------+

SELECT DISTINCT EMIRATES FROM CLASSLIST;


+--------------+
| EMIRATES |
+--------------+
| DUBAI |
| SHARJAH |
+--------------+
Q1: Write a query to display number of Emirates
from tables classlist.

Q2: Write a query to display number of unique


Emirates from tables classlist.
UPDATE Command
This command is used to implement modification of the data
values.
Syntax:
UPDATE <table name>
SET <column name1>=new value, <column name>=new value etc
[WHERE <condition>];

Q1: Write a query to add 10 marks to sub1 for student Aaditya.


Q2. Display the updated table.
Q3. Write a query to subtract 10 marks from sub1 for student
Aaditya.
Q4. Display the updated table.
UPDATE Command
UPDATE Command
Insert the following record in the table
classlist
6 ,’ABC’, ’2000-12-20’, 100, 100, 100, 100.0,
‘111111’, ‘UAQ’
Insert the following record in the table classlist

6 ,’ABC’, ’2000-12-20’, 100, 100, 100, 100.0, ‘111111’, ‘UAQ’


Write a SQL statement to delete the student
record where roll number is 6.
ALTER TABLE command
This command is used to implement modification of the structure of the table. This is
a DDL command. Using this command, we can add a new column, remove the
existing column and modify data type of existing column.

Syntax:
ALTER TABLE <table name> [ADD/MODIFY/DROP] <column name>;
For example:
1. Add one new column totalfees with number (10, 2).
ALTER TABLE student ADD totalfees number(10,2);
ALTER TABLE table_name ADD column_name datatype --add column

2. Change totalfees datatype as number(12,2).


ALTER TABLE student MODIFY totalfees number(12,2);
ALTER TABLE table_name MODIFY COLUMN column_name datatype--MySQL
ALTER TABLE table_name MODIFY column_name datatype –Oracle/MySQL

3. Remove totalfees column.


ALTER TABLE student DROP totalfees;
ALTER TABLE table_name DROP COLUMN column_name --delete column
Write a statement to add a new column Hobby varchar(10) to table
classlist
Write a statement to add a new column Hobby varchar(10) to table classlist
Write a statement to modify column size of Hobby to varchar(15) in table classlist
Write a statement to modify column size of Hobby to varchar(15) in table classlist
Write a SQL statement to change column name HOBBY to ACTIVITY
Write a SQL statement to change column name HOBBY to ACTIVITY
DROP TABLE Command
This command is used to remove the entire structure
of the table and information. This is also from the DDL
command.

Syntax:
DROP TABLE<table name>;

For example:
Remove the whole structure of MYTABLE table.
DROP TABLE MYTABLE;
SQL DROP CONSTRAINT Keyword
DROP CONSTRAINT
The DROP CONSTRAINT command is used to delete
a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK
constraint.
DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following
SQL:
SQL Server / Oracle / MS Access:
ALTER TABLE Persons DROP CONSTRAINT PK_Person;
MySQL:
ALTER TABLE Persons DROP PRIMARY KEY;
Write a query to drop the primary key
from table classlist.
Write a query to drop the primary key
from table classlist.
mysql> ALTER TABLE classlist DROP PRIMARY KEY;
SQL ADD CONSTRAINT Keyword
ADD CONSTRAINT
The ADD CONSTRAINT command is used to create a
constraint after a table is already created.
The following SQL adds a constraint named
"PK_Person" that is a PRIMARY KEY constraint on
multiple columns (ID and LastName):
Example
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY
(ID,LastName);
Write a query to ADD constraint to make
ROLL as primary key in table classlist.
Write a query to ADD constraint to make
ROLL as primary key in table classlist.
mysql> ALTER TABLE classlist ADD CONSTRAINT
PRIMARY KEY (ROLL);
OR
mysql> ALTER TABLE classlist ADD CONSTRAINT
PK_ROLL PRIMARY KEY (ROLL);
Write a statement to change column name activity to
Game in table classlist.
Write a query to perform Equi-Join operation on
Tables CUST and BAL to display the name of the
customer and balance.
Write a query to perform Equi-Join operation on
Tables CUST and BAL to display the name of the
customer and balance.
mysql> SELECT NAME, BALANCE FROM CUST, BAL WHERE
CUST.ACCT=BAL.ACCT;

mysql> SELECT CUST.NAME, BAL.BALANCE FROM CUST,


BAL WHERE CUST.ACCT=BAL.ACCT;

mysql> SELECT NAME, BALANCE FROM CUST C, BAL B


WHERE C.ACCT=B.ACCT;
What will be the output of the following
query? Will the output be same for both
queries or different?

mysql> SELECT * FROM CUST, BAL WHERE


CUST.ACCT=BAL.ACCT;

mysql> SELECT * FROM BAL, CUST WHERE


CUST.ACCT=BAL.ACCT;
What will be the output of the following
query:
mysql> SELECT * FROM CUST, BAL;
It produces Cartesian
Product to two tables
where cardinality=
cardinality of 1st table
X Cardinality of the 2nd
table
And degree = Degree
of 1st table + Degree of
2nd table
Table CUST has
Cardinality=4 &
Degree=2
Table BAL has
Cardinality=4 &
degree=2.
CUST X BAL has
Cardinality=4X4=16 &
Degree=2+2=4
Write queries for the following operations:
• Insert a new customer name Rahul in Table CUST
• Update the account number of Rahul as 5
This format INSERT can
be used to enter
selected column values
in a table by specifying
column names in 1st
bracket after table name
and corresponding
values in another
bracket after keyword
VALUES
What will be the Degree and
Cardinality of Cartesian product
of CUST X BAL?
What will be the Degree and
Cardinality of Cartesian product
of CUST X BAL?
• ge

Degree= 2+2=4 and


Cardinality= 5*4=20
Write queries to perform the equi-join
operation on two tables CUST & BAL
Write queries to perform the equi-join
operation on two tables CUST & BAL

mysql> SELECT * FROM cust, bal WHERE


cust.acct=bal.acct;

mysql> SELECT * FROM cust JOIN bal ON


cust.acct=bal.acct;
Write queries to perform Natural Join on
two tables CUST & BAL.

mysql> SELECT name, balance FROM cust


NATURAL JOIN bal;

mysql> SELECT * FROM cust NATURAL JOIN bal;


SQL Equi Join
What is Equi Join in SQL?
• SQL EQUI JOIN performs a JOIN against equality
or matching column(s) values of the associated
tables. An equal sign (=) is used as comparison
operator in the where clause to refer equality.
• You may also perform EQUI JOIN by using JOIN
keyword followed by ON keyword and then
specifying names of the columns along with their
associated tables to check equality.
Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
or
SELECT *
FROM table1
JOIN table2
[ON (join_condition)]
Example: Equi Join
Example: Equi Join
Note:
1. Inner join can have equality (=) and other
operators (like <,>,<>) in the join condition.
2. Equi join only have equality (=) operator in the
join condition.
SQL Natural Join
What is Natural Join in SQL?
• We have already learned that an EQUI JOIN performs a
JOIN against equality or matching column(s) values of
the associated tables and an equal sign (=) is used as
comparison operator in the where clause to refer
equality.
• The SQL NATURAL JOIN is a type of EQUI JOIN and is
structured in such a way that, columns with the same
name of associated tables will appear once only.
Natural Join: Guidelines
• - The associated tables have one or more pairs of
identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
Syntax:
SELECT *
FROM table1
NATURAL JOIN table2;
Natural Join Example

In the above join result, we have only one column


“CCODE" for each pair of equally named columns.
Note
• In Natural join, you can't see what columns
from both the tables will be used in the join.
In Natural join, you might not get the desired
result what you are expecting.
https://www.youtube.com/watch?v=OPqsVSg6n
UA&t=64s
DROP Table
Drop - To destroy an existing database, table, index,
or view.
A DROP statement in SQL removes an object from a
relational database management system (RDBMS).
The types of objects that can be dropped depends
on which RDBMS is being used, but most support
the dropping of tables, users, and databases.
The typical usage is simply:
DROP TABEL table_name;
For example, the command to drop a table named
employees would be:
DROP TABLE employees;
DROP Table
The DROP statement is distinct from the
DELETE and TRUNCATE statements, in that
DELETE and TRUNCATE do not remove the
table itself. For example, a DELETE
statement might delete some (or all) data
from a table while leaving the table itself
in the database, whereas a DROP
statement would remove the entire table
from the database.
SQL ALTER TABLE
Adding column(s) to a table
Syntax #1
To add a column to an existing table, the SQL ALTER TABLE
syntax is:
ALTER TABLE table_name ADD column_name column-
definition;

For example:
ALTER TABLE supplier ADD supplier_name varchar2(50);
This will add a column called supplier_name to the
supplier table.
SQL ALTER TABLE
Adding column(s) to a table
Syntax #2
To add multiple columns to an existing table, the SQL
ALTER TABLE syntax is:
ALTER TABLE table_name ADD (column_1 column-
definition, column_2 column-definition, ... column_n
column_definition);

For example:
ALTER TABLE supplier ADD (supplier_name varchar2(50),
city varchar2(45));
This will add two columns (supplier_name and city) to
the supplier table.
ALTER TABLE
Modifying column(s) in a table
Syntax #1
To modify a column in an existing table, the SQL ALTER
TABLE syntax is:
ALTER TABLE table_name MODIFY column_name
column_type;

For example:
ALTER TABLE supplier MODIFY supplier_name
varchar2(100) not null;
This will modify the column called supplier_name to be
a data type of varchar2(100) and force the column to not
allow null values.
ALTER TABLE
Modifying column(s) in a table
Syntax #2
To modify multiple columns in an existing table, the SQL
ALTER TABLE syntax is:
ALTER TABLE table_name MODIFY (column_1
column_type, column_2 column_type, ... column_n
column_type);

For example:
ALTER TABLE supplier MODIFY (supplier_name
varchar2(100) not null, city varchar2(75));
This will modify both the supplier_name and city
columns.
SQL ALTER TABLE
Drop column(s) in a table
Syntax
To drop a column in an existing table, the SQL ALTER
TABLE syntax is:
ALTER TABLE table_name DROP COLUMN
column_name;

For example:
ALTER TABLE supplier DROP COLUMN
supplier_name;
This will drop the column called supplier_name
from the table called supplier.
SQL ALTER TABLE
Rename column(s) in a table
(NEW in Oracle 9i Release 2)
Syntax #1
Starting in Oracle 9i Release 2, you can now rename a
column. To rename a column in an existing table, the
SQL ALTER TABLE syntax is:
ALTER TABLE table_name RENAME COLUMN old_name
to new_name;

For example:
ALTER TABLE supplier RENAME COLUMN
supplier_name to sname;
This will rename the column called supplier_name to
sname.
SQL: SELECT Statement
The SQL SELECT statement allows you to
retrieve records from one or more tables in
your SQL database.

The syntax for the SQL SELECT statement is:

SELECT columns FROM tables WHERE


predicates;
SQL: SELECT Statement
SQL SELECT Statement - Select all fields from one
table example

Let's take a look at how to use the SQL SELECT


statement to select all fields from a table.

SELECT * FROM suppliers WHERE city = 'Newark';

In this SQL SELECT statement, we've used * to


signify that we wish to view all fields from the
suppliers table where the supplier resides in
Newark.
SQL: SELECT Statement
SQL SELECT Statement - Selecting individual fields from one table
example

You can also use the SQL SELECT statement to select individual
fields from the table, as opposed to all fields from the table.

For example:

SELECT name, city, state FROM suppliers WHERE supplier_id > 1000;

This SQL SELECT statement would return only the name, city, and
state fields from the suppliers table where the supplier_id value is
greater than 1000.
SQL: SELECT Statement
SQL SELECT Statement - Select fields from multiple tables
example

You can also use the SQL SELECT statement to retrieve fields
from multiple tables.

SELECT orders.order_id, suppliers.name FROM suppliers


INNER JOIN orders ON suppliers.supplier_id =
orders.supplier_id;

This SQL SELECT statement joins two tables together to


gives us a result set that displays the order_id and supplier
name fields where the supplier_id value existed in both the
suppliers and orders table.
SQL: INSERT Statement
SQL: INSERT Statement
The SQL INSERT statement allows you to insert a
single record or multiple records into a table.

The syntax for the SQL INSERT statement is:

INSERT INTO table (column-1, column-2, ...


column-n) VALUES (value-1, value-2, ... value-n);

INSERT INTO table VALUES (value-1, value-2, ...


value-n);
SQL: INSERT Statement
SQL INSERT Statement - Using VALUES keyword
example
The simplest way to create an SQL INSERT statement to
list the values using the VALUES keyword.

For example:
INSERT INTO suppliers (supplier_id, supplier_name)
VALUES (24553, 'IBM');

This SQL INSERT statement would result in one record


being inserted into the suppliers table. This new
record would have a supplier_id of 24553 and a
supplier_name of IBM.
SQL: UPDATE Statement
The SQL UPDATE statement allows you to
update a single record or multiple records
in a table.
The syntax for the SQL UPDATE statement
is:
UPDATE table SET column = expression
WHERE predicates;
SQL UPDATE - example
Let's take a look at a very simple example.

UPDATE suppliers SET name = 'HP'


WHERE name = 'IBM';

This SQL UPDATE statement would


update all supplier names in the suppliers
table from IBM to HP.
SQL UPDATE - Updating multiple
columns example
Let's take a look at an SQL UPDATE example where you might
want to update more than one column with a single SQL
UPDATE statement.

UPDATE suppliers SET name = 'Apple', product = 'iPhone'


WHERE name = 'Rim';

When you wish to update multiple columns, you can do this


by separating the column/value pairs with commas.

This SQL UPDATE statement would update the supplier name


to "Apple" and product to "iPhone" where the name of the
supplier is "Rim".
SQL: DELETE Statement
The SQL DELETE statement allows you to
delete a single record or multiple records
from a table.

The syntax for the SQL DELETE statement


is:
DELETE FROM table WHERE predicates;
SQL DELETE Statement - One condition
example
Let's take a look at a simple example, where we just
have one condition in our SQL DELETE statement:
DELETE FROM suppliers WHERE supplier_name =
'IBM';
This SQL DELETE statement would delete all records
from the suppliers table where the supplier_name is
IBM.
You may wish to check for the number of rows that will
be deleted. You can determine the number of rows that
will be deleted by running the following SQL SELECT
statement before performing the delete.
SELECT count(*) FROM suppliers WHERE
supplier_name = 'IBM';

You might also like