KEMBAR78
Fundamental of Database SQL Presentation | PDF | Sql | Table (Database)
0% found this document useful (0 votes)
10 views81 pages

Fundamental of Database SQL Presentation

This document provides an introduction to SQL (Structured Query Language), detailing its purpose, commands, and functionalities for managing databases. It covers various SQL command types including DDL, DML, DCL, and DQL, as well as specific commands like CREATE, SELECT, INSERT, UPDATE, DELETE, and JOIN operations. Additionally, it explains the syntax and usage of SQL commands with examples for better understanding.

Uploaded by

kaleabashenafi5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views81 pages

Fundamental of Database SQL Presentation

This document provides an introduction to SQL (Structured Query Language), detailing its purpose, commands, and functionalities for managing databases. It covers various SQL command types including DDL, DML, DCL, and DQL, as well as specific commands like CREATE, SELECT, INSERT, UPDATE, DELETE, and JOIN operations. Additionally, it explains the syntax and usage of SQL commands with examples for better understanding.

Uploaded by

kaleabashenafi5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

St.

Mary’s University
Faculty of xxxxxx
Department of xxxx
December, 2024

21/12/2024 SMU Course Name 1


Introduction
What is SQL?
SQL stands for Structured Query Language

SQL lets you access and manipulate databases

SQL became a standard of the American National


Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in
1987

21/12/2024 SMU Course Name 2


Introduction
Using SQL commands?
create new databases
create new tables in a database
create stored procedures in a database
create views in a database
set permissions on tables, procedures, and views
execute queries against a database
retrieve data from a database
insert records in a database
update records in a database
delete records from a database

22/12/2024 SMU Course Name 3


Elements of data base
Structured Query Language
1. Tables
✔ In relational database systems (DBS) data are
represented using tables (relations).
✔ table is uniquely identified by its name and consists of
rows that contain the stored information
✔ each row containing exactly one tuple (or record).
✔ A table can have one or more columns. A column is
made up of a column name and a data type, and it
describes an attribute of the tuples.

21/12/2024 SMU Course Name 4


Commands
These commands can be classified into groups based on
their nature:
DDL - Data Definition Language:

DML - Data Manipulation Language:

DCL - Data Control Language:

DQL - Data Query Language:

21/12/2024 SMU Course Name 5


DDL
Commands and Description of DDL
1) CREATE Creates a new table, a view of a table, or
other object in database
2) ALTER Modifies an existing database object, such as
a table.
3) DROP Deletes an entire table, a view of a table or
other object in the database.

21/12/2024 SMU Course Name 6


DML
Commands and Description
1) INSERT Creates a record

2) UPDATE Modifies records

3) DELETE Deletes records

21/12/2024 SMU Course Name 7


DCL
Commands and Description
1) GRANT Gives a privilege to user

2) REVOKE Takes back privileges granted from user

21/12/2024 SMU Course Name 8


SQL Commands:
The standard SQL commands to interact with
relational databases are
CREATE,
SELECT,
INSERT,
UPDATE,
DELETE and
DROP.
These commands areclassified into groups based on
their nature:

21/12/2024 SMU Course Name 9


Purpose Of SQL
Why SQL?
Because SQL Allows users
to access data in relational database management
systems.
to describe the data.
to define the data in database and manipulate that data.
to embed within other languages using SQL modules,
libraries & pre-compilers.
to create and drop databases and tables.
to create view, stored procedure, functions in a database.
to set permissions on tables, procedures and views

21/12/2024 SMU Course Name 10


DQL - Data Query Language:
Data Query Language:

Command and Description

1) SELECT Retrieves certain records from one or more


tables

21/12/2024 SMU Course Name 11


SELECT…
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
Syntax
SELECT column1, column2, ...
FROM table_name;

❖ Here, column1, column2, ... are the field names of the table
you want to select data from.
❖ The table_name represents the name of the table you want to
select data from.

21/12/2024 SMU Course Name 12


SELECT…
Syntax

SELECT column_names
FROM table_name
[WHERE condition];

21/12/2024 SMU Course Name 13


SELECT…
Example

SELECT * FROM Customers;


__________//______
SELECT * FROM product
WHERE p_name = ‘Cake';
__________//______
SELECT p_name, price
FROM product;

21/12/2024 SMU Course Name 14


SELECT…
Subqueries (nested query)
A query result can also be used in a condition of a where
clause.
SELECT ENAME, SAL
FROM EMP WHERE EMPNO in
(SELECT PMGR FROM PROJECT
WHERE PSTART < ’31-DEC-90’) AND DEPTNO =20;

List the name and salary of employees of the department 20


who are leading a project that started before December 31,
1990:

21/12/2024 SMU Course Name 15


SELECT…
Subqueries (nested query)

SELECT ∗ FROM EMP


WHERE DEPTNO IN
(SELECT DEPTNO FROM DEPT
WHERE LOC = ’BOSTON’);

List all employees who are working in a department


located in BOSTON:

21/12/2024 SMU Course Name 16


SELECT…
Subqueries (nested query)

SELECT ∗ FROM EMP


WHERE SAL >= ANY
(SELECT SAL FROM EMP
WHERE DEPTNO = 30) AND DEPTNO = 10;

Retrieve all employees who are working in


department 10 and who earn at least as much as any
(i.e., at least one) employee working in department
30:

21/12/2024 SMU Course Name 17


SELECT…
Subqueries (nested query)

Example
SELECT ∗ FROM EMP
WHERE SAL > ALL
(SELECT SAL FROM EMP
WHERE DEPTNO = 30) AND DEPTNO <> 30;

List all employees who are not working in department 30


and who earn more than all employees working in
department 30:

21/12/2024 SMU Course Name 18


SELECT…
SQL Aliases
• SQL aliases are used to give a table, or a column in a
table, a temporary name.
• Aliases are often used to make column names more
readable.
• An alias only exists for the duration of that query.
• An alias is created with the AS keyword.

21/12/2024 SMU Course Name 19


SELECT…
SQL Aliases

Syntax
SELECT column_name AS alias_name
FROM table_name;

SELECT CustomerID AS ID
FROM Customers;

21/12/2024 SMU Course Name 20


SELECT…
Grouping
Syntax

SELECT <column(s)>
FROM <table(s)>
WHERE <condition>
GROUP BY <group column(s)>
[HAVING <group condition(s)>];

21/12/2024 SMU Course Name 21


SELECT…
Grouping
Example

SELECT Department, sum(Salary) AS Salary


FROM employee
GROUP BY department
HAVING SUM (Salary) >= 50000;

21/12/2024 SMU Course Name 22


SELECT…
Grouping
Example

SELECT Department, sum(Salary) AS Salary


FROM employee
GROUP BY department
HAVING SUM (Salary) >= 50000;

21/12/2024 SMU Course Name 23


SELECT…

The SQL Join Clause…

21/12/2024 SMU Course Name 24


SQL JOIN
SQL JOIN
• A JOIN clause is used to combine rows from two or
more tables, based on a related column between
them.
• Different Types of SQL JOINs
INNER JOIN:
LEFT (OUTER) JOIN:
RIGHT (OUTER) JOIN
FULL (OUTER) JOIN:

21/12/2024 SMU Course Name 25


INNER JOIN…
1. INNER JOIN:
Returns records that have matching values in both tables

21/12/2024 SMU Course Name 26


FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

INNER JOIN…

Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

21/12/2024 SMU Course Name 27


INNER JOIN…
Example
Join Products and Categories with the INNER JOIN keyword:

SELECT ProductID, ProductName, CategoryName


FROM Products
INNER JOIN Categories ON Products.CategoryID =
Categories.CategoryID;
The INNER JOIN keyword returns only rows with a match in both tables. Which means
that if you have a product with no CategoryID, or with a CategoryID that is not present in
the Categories table, that record would not be returned in the result.

21/12/2024 SMU Course Name 28


INNER JOIN…
CategoryID CategoryName Description
1 Beverages Soft drinks, coffees,
teas, beers, and ales
2 Condiments Sweet and savory
sauces, relishes,

3
End….
Confections
spreads, and seasonings
Desserts, candies, and
sweet breads

ProductID ProductName CategoryID Price


1 Chais 1 18
2 Chang 1 19
3 Aniseed Syrup 2 10
21/12/2024 SMU Course Name 29
INNER JOIN…

ProductID ProductName CategoryName


39 Chartreuse verte Beverages
2 Chang Beverages
24 Guaraná Fantástica Beverages
34 Sasquatch Ale Beverages
35 Steeleye Stout Beverages
1 Chais Beverages
38 Côte de Blaye Beverages

21/12/2024 SMU Course Name 30


LEFT (OUTER) JOIN
2. LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table

21/12/2024 SMU Course Name 31


LEFT (OUTER) JOIN…
Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from
the right side, if there is no match.

21/12/2024 SMU Course Name 32


LEFT (OUTER) JOIN…
Example

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
LEFT JOIN Orders ON Customers.CustomerID =
Orders.CustomerID
ORDER BY Customers.CustomerName;

The LEFT JOIN keyword returns all records from the left
table (Customers), even if there are no matches in the right
table (Orders).
21/12/2024 SMU Course Name 33
OrderID
LEFT
CustomerID
(OUTER)
EmployeeID
JOIN…
OrderDate ShipperID
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2

CustomerI Custom Contact Address City PostalC Country


D erName Name ode
1 Alfreds Maria Obere Berlin 12209 German
Futterkis Anders Str. 57 y
te
2 Ana Ana Avda. México 05021 Mexico
Trujillo Trujillo de la D.F.
Empare Constitu
dados y ción
helados 2222
321/12/2024 Antonio Antonio Matader
SMU México 05023
Course Name Mexico 34
LEFT (OUTER) JOIN…
CustomerName OrderID
Alfreds Futterkiste
Ana Trujillo Emparedados y helados 10308
Antonio Moreno Taquería 10365
Around the Horn 10383
Around the Horn 10355
Berglunds snabbköp 10278
Berglunds snabbköp 10280
Berglunds snabbköp 10384
Blauer See Delikatessen
Blondel père et fils 10360
Blondel père et fils 10297
Blondel père et fils 10436
Blondel père et fils 10265
Bólido Comidas preparadas 10326
Bon app' 10331
Bon app' 10340
Bon app' 10362

21/12/2024 SMU Course Name 35


RIGHT (OUTER) JOIN
3. RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table

21/12/2024 SMU Course Name 36


RIGHT (OUTER) JOIN: …
Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

The RIGHT JOIN keyword returns all records from the right table (table2), and the
matching records from the left table (table1). The result is 0 records from the left side,
if there is no match.

21/12/2024 SMU Course Name 37


RIGHT (OUTER) JOIN: …
Example

SELECT Orders.OrderID, Employees.LastName,


Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
ORDER BY Orders.OrderID;

The RIGHT JOIN keyword returns all records from the right
table (Employees), even if there are no matches in the left
table (Orders).
21/12/2024 SMU Course Name 38
RIGHT (OUTER) JOIN: …
OrderID CustomerID EmployeeID OrderDate ShipperID
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2

EmployeeID LastName FirstName BirthDate Photo


1 Davolio Nancy 12/8/1968 EmpID1.pic
2 Fuller Andrew 2/19/1952 EmpID2.pic
3 Leverling Janet 8/30/1963 EmpID3.pic

21/12/2024 SMU Course Name 39


RIGHT (OUTER) JOIN: …

OrderID LastName FirstName


West Adam
10248 Buchanan Steven
10249 Suyama Michael
10250 Peacock Margaret
10251 Leverling Janet
10252 Peacock Margaret
10253 Leverling Janet
10254 Buchanan Steven

21/12/2024 SMU Course Name 40


FULL (OUTER) JOIN
4. FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table

21/12/2024 SMU Course Name 41


FULL (OUTER) JOIN…
Syntax
• SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

• The RIGHT JOIN keyword returns all records from


the right table (Employees), even if there are no
matches in the left table (Orders).
21/12/2024 SMU Course Name 42
FULL (OUTER) JOIN…
Example
The following SQL statement selects all customers, and all orders:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

The FULL OUTER JOIN keyword returns all matching records from both tables
whether the other table matches or not. So, if there are rows in "Customers"
that do not have matches in "Orders", or if there are rows in "Orders" that do
not have matches in "Customers", those rows will be listed as well.

21/12/2024 SMU Course Name 43


FULL (OUTER) JOIN…
OrderID CustomerID EmployeeID OrderDate ShipperID
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2

Custome Custome ContactN Address City PostalCo Country


rID rName ame de
1 Alfreds Maria Obere Berlin 12209 Germany
Futterkist Anders Str. 57
e
2 Ana Ana Avda. de México 05021 Mexico
Trujillo Trujillo la D.F.
Empared Constituc
ados y ión 2222
helados
21/12/2024 SMU Course Name 44
3 Antonio Antonio Matadero México 05023 Mexico
FULL (OUTER) JOIN…

CustomerName OrderID
Null 10309
Null 10310
Alfreds Futterkiste Null
Ana Trujillo Emparedados y helados 10308
Antonio Moreno Taquería Null

21/12/2024 SMU Course Name 45


SQL JOIN

End….

21/12/2024 SMU Course Name 46


SQL CREATE …
SQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a


new SQL database.

Syntax

CREATE DATABASE databasename;

21/12/2024 SMU Course Name 47


SQL CREATE …
The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a new
table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

21/12/2024 SMU Course Name 48


SQL CREATE …
SQL DROP Database Statement
Following are the important points to remember
before you delete an existing database −
Make sure you have taken proper backup of thew
database before you delete it.
Make sure no other application is connected and
using this database.
Make sure you have the necessary privilege to delete
the database. Usually an admin can delete the
databaase.

21/12/2024 SMU Course Name 49


SQL CREATE …
SQL DROP Database Statement
Syntax
DROP DATABASE DatabaseName;

Or

DROP DATABASE IF EXISTS DatabaseName;

21/12/2024 SMU Course Name 50


SQL CREATE …
Syntax
CREATE TABLE <table> (
<column 1> <DATA TYPE> [not null] [UNIQUE] [<column
CONSTRAINT>],
.........
<column n> <DATA TYPE> [NOT NULL] [UNIQUE]
[<column CONSTRAINT>],
[<TABLE CONSTRAINT(S)>]
);

21/12/2024 SMU Course Name 51


SQL CREATE …
Example

CREATE TABLE PERSONS (


PersonID INT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);

21/12/2024 SMU Course Name 52


SQL CREATE …
Example

CREATE TABLE EMP (


EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(30) NOT NULL,
JOB VARCHAR2(10),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2),
DEPTNO NUMBER(2)
);

21/12/2024 SMU Course Name 53


SQL Delete Table
The SQL DROP Table Statement
The SQL DROP TABLE statement is a Data Definition Language (DDL) command that is
used to remove a table's definition, and its data, indexes, triggers, constraints and
permission specifications (if any).
Note −
You should be very careful while using this command because once a table is deleted
then all the information available in that table will also be lost forever.
If the table is partitioned, the statement removes the table definition, all its
partitions, all data stored in those partitions, and all partition definitions.
To drop a table in a database, one must require ALTER permission on the said table
and CONTROL permissions on the table schema.
Even though it is a data definition language command, it is different from TRUNCATE
TABLE statement as the DROP statement completely frees the table from the
memory.
DROP TABLE causes an implicit commit, except when used with the TEMPORARY
keyword.

21/12/2024 SMU Course Name 54


SQL Delete Table
Difference b/w DROP and TRUNCATE statements
When you use the drop statement it deletes the table's row
together with the table's definition so all the relationships of
that table with other tables will no longer be valid.
When you drop a table:
Table structure will be dropped
Relationship will be dropped
Integrity constraints will be dropped
Access privileges will also be dropped
On the other hand when we TRUNCATE a table, the table
structure remains the same, so you will not face any of the
above problems.

21/12/2024 SMU Course Name 55


SQL Delete Table
The SQL DROP Table Statement
Syntax
DROP TABLE table_name;
Or
DROP TABLE [IF EXISTS] table_name;

TRUNCATE TABLE employee;

21/12/2024 SMU Course Name 56


SQL Delete Table
SQL TRUNCATE TABLE
A truncate SQL statement is used to remove all
rows (complete data) from a table.
It is similar to the DELETE statement with no
WHERE clause.
Syntax
TRUNCATE TABLE table_name;

21/12/2024 SMU Course Name 57


DELETE and TRUNCATE
• There is a slight difference b/w delete and
truncate statement.
• The DELETE statement only deletes the rows from
the table based on the condition defined by
WHERE clause or delete all the rows from the
table when condition is not specified.
• But it does not free the space containing by the
table.
• The TRUNCATE statement: it is used to delete all
the rows from the table and free the containing
space.

21/12/2024 SMU Course Name 58


DROP COLUMN
ALTER TABLE − DROP COLUMN
• If you need to drop an existing column from a table,
you should use the DROP COLUMN option along with
ALTER TABLE statement as shown below.
Syntax
ALTER TABLE table_name DROP COLUMN
column_name;
• ALTER TABLE customers DROP COLUMN SEX;

21/12/2024 SMU Course Name 59


SQL DELETE TABLE
SQL DELETE TABLE
• The DELETE statement is used to delete rows from a
table. If you want to remove a specific row from a
table you should use WHERE condition.
• Syntax
DELETE FROM table_name [WHERE condition];

21/12/2024 SMU Course Name 60


Constraints
Following are commonly used constraints available in SQL:
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is
specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database
table.
CHECK Constraint: The CHECK constraint ensures that all values in a column
satisfy certain conditions.
INDEX: Use to create and retrieve data from the database very quickly.

21/12/2024 SMU Course Name 61


Constraints
CREATE TABLE table_name
(column_1 data_type_1,
column_2 data_type_2, ...
PRIMARY KEY (columns) );

21/12/2024 SMU Course Name 62


Constraints
CREATE TABLE table_name
(column_1 data_type_1,
column_2 data_type_2, ...
PRIMARY KEY (columns) );

21/12/2024 SMU Course Name 63


Constraints

CREATE TABLE CUSTOMERS


( ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2) DEFAULT 5000.00,
PRIMARY KEY (ID) );
21/12/2024 SMU Course Name 64
Constraints
Drop Default Constraint:
To drop a DEFAULT constraint, use the
following SQL:
ALTER TABLE CUSTOMERS
ALTER COLUMN SALARY DROP DEFAULT;

21/12/2024 SMU Course Name 65


SQL INSERT STATEMENT
• It is used to insert a single or a multiple
records in a table.
• There are two ways to insert data in a table:
1. By SQL insert into statement
By specifying column names
Without specifying column names
2. By SQL insert into select statement

21/12/2024 SMU Course Name 66


Constraints
FOREIGN Key:
Example:
Consider the structure of the two tables as follows:
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL, ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );

21/12/2024 SMU Course Name 67


Constraints
FOREIGN Key:
Example:
ORDERS table:
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);

21/12/2024 SMU Course Name 68


Constraints
FOREIGN Key:
Example:
If ORDERS table has already been created, and the foreign key
has not yet been set, use the syntax for specifying a foreign key
by altering a table.

ALTER TABLE ORDERS ADD FOREIGN KEY (Customer_ID)


REFERENCES CUSTOMERS (ID);

21/12/2024 SMU Course Name 69


Constraints
FOREIGN Key:
Example:
DROP a FOREIGN KEY Constraint:
To drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE ORDERS


DROP FOREIGN KEY;

21/12/2024 SMU Course Name 70


Constraints
CHECK Constraint:
The CHECK Constraint enables a condition to check
the value being entered into a record.

If the condition evaluates to false, the record violates


the constraint and isn’t entered into the table.

21/12/2024 SMU Course Name 71


Constraints
CHECK Constraint:
For example,
the following SQL creates a new table
called CUSTOMERS and adds five
columns.
Here, we add a CHECK with AGE column,
so that you can not have any CUSTOMER
below 18 years:
21/12/2024 SMU Course Name 72
Constraints
CHECK Constraint:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

21/12/2024 SMU Course Name 73


Constraints
CHECK Constraint:

ALTER TABLE CUSTOMERS MODIFY AGE INT NOT NULL CHECK


(AGE >= 18 );

21/12/2024 SMU Course Name 74


SQL INSERT STATEMENT
SQL INSERT STATEMENT
• It is used to insert a single or a multiple records in
a table.
• There are two ways to insert data in a table:
1. By SQL insert into statement
1) By specifying column names
2) Without specifying column names
2. By SQL insert into select statement

21/12/2024 SMU Course Name 75


SQL INSERT STATEMENT
1. Inserting data directly into a table
• You can insert a row in the table by using SQL
INSERT INTO command.
There are two ways to insert values in a table.
I. In the first method there is no need to
specify the column name where the
data will be inserted, you need only
their values.
– INSERT INTO table_name
– VALUES (value1, value2, value3....);

21/12/2024 SMU Course Name 76


SQL INSERT STATEMENT
II. The second method specifies both the column name and
values which you want to insert.
INSERT INTO table_name (column1, column2, column3....)
VALUES (value1, value2, value3.....);
Example
1. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (1, ABHIRAM, 22, ALLAHABAD);
2. INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (2, ALKA, 20, GHAZIABAD);

21/12/2024 SMU Course Name 77


SQL INSERT STATEMENT
2) Inserting data through SELECT Statement
Syntax
INSERT INTO table_name
[(column1, column2, .... column)]
SELECT column1, column2, .... Column N
FROM table_name [WHERE condition];
Note: when you add a new row, you should make sure that data type of the
value and the column should be matched.

21/12/2024 SMU Course Name 78


SQL INSERT STATEMENT
Copy all columns from one table to another table:

INSERT INTO table2


SELECT * FROM table1
WHERE condition;

21/12/2024 SMU Course Name 79


SQL INSERT STATEMENT
Copy only some columns from one table into another
table:

INSERT INTO table2 (column1, column2, column3, ...)


SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

21/12/2024 SMU Course Name 80


SQL INSERT STATEMENT
Example
Copy "Suppliers" into "Customers" (the columns that are
not filled with data, will contain NULL):

INSERT INTO Customers (CustomerName, City, Country)


SELECT SupplierName, City, Country FROM Suppliers;

21/12/2024 SMU Course Name 81

You might also like