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

Week13 Database Commands

The document discusses various SQL statements used for data definition and manipulation. It covers the CREATE, ALTER, DROP, TRUNCATE and RENAME statements used for defining database components. It also covers the INSERT statement used for inserting data into tables.

Uploaded by

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

Week13 Database Commands

The document discusses various SQL statements used for data definition and manipulation. It covers the CREATE, ALTER, DROP, TRUNCATE and RENAME statements used for defining database components. It also covers the INSERT statement used for inserting data into tables.

Uploaded by

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

`

SQL Statements
Objectives

• Familiarize SQL Statements:

2
CREATE Command

• Data Definition Language (DDL) SQL command.


• Used to create a table or a database in RDBMS.
• There are two CREATE statements in SQL:
▪ CREATE DATABASE
▪ CREATE TABLE

3
CREATE DATABASE
• A database is defined as a structured set of data.
• To create a database in RDBMS, create command is used.
Syntax : CREATE DATABASE <Database_Name> ;

Keyword Keyword Name of new Database Every SQL Query ends


with a semicolon

Example: Creating DATABASE in pgAdmin

4
CREATE TABLE
• Create command can also be used to create tables.
• Specify the details of the columns of the tables.
• Specify the names and datatypes of various columns.

Syntax : CREATE TABLE without table structure

CREATE TABLE Table_Name () ;

Keyword Keyword Name of parenthesis Every SQL Query


Table ends with a
semicolon

5
Example:
Creating a TABLE without table structure

6
CREATE TABLE

Syntax : CREATE TABLE with table structure

CREATE TABLE <Table_Name> (column1 datatype,


;
column2 datatype,….)
Keyword Keyword Name of Every SQL
Table Name and Datatype of Query ends
columns present in the with a
tables semicolon

7
Example:
Creating a TABLE with table structure

8
Data Types

9
DROP TABLE

• It will destroy the table and all data which will be recorded in it.

Syntax : DROP TABLE Table_Name ;

Keyword Keyword Name of Table Every SQL Query ends


with a semicolon

10
Example: Drop a table in a database
1 List of tables in Database

3
Updated list of tables in Database
2 DROP Command

11
“ALTER TABLE” Statement
• This statement is used to add, modify or delete
constraints or columns.
Syntax : ALTER TABLE <Table_Name> Action ;

Keyword Keyword Name of the Specify the Every SQL Query ends
Table to be action to be with a semicolon
altered/ modify done on
Action table

➢ Add a column
➢ Drop a column ➢ Set a default value for the column.
➢ Change the data type of a column ➢ Add a constraint to a column.
➢ Rename a column ➢ Rename a table

12
Example: Add a column to a Table
Syntax : ALTER TABLE table_name ADD COLUMN
new_column_name data_type constraint;

Table : Department

13
TRUNCATE TABLE Statement

• To remove all data from a table, you use the DELETE statement.
• However, when you use the DELETE statement to delete all data
from a table that has a lot of data, it is not efficient.
• Therefore, you need to use the TRUNCATE TABLE statement.
Syntax : TRUNCATE TABLE <Table_Name> ;

Keyword Keyword Name of the Table Every SQL Query ends


with a semicolon

14
TRUNCATE :
Remove all data from multiple tables
• To remove all data from multiple tables at once, you separate
each table by a comma (,) .

Syntax :
TRUNCATE TABLE <Table_Name1>, ;
<Table_Name2>, …
Keyword Keyword Every SQL Query ends
with a semicolon
Names of the Table
to delete the data

15
RENAME Command

• It is used to set a new name for any existing table.

Syntax :
RENAME TABLE <Old_Table_Name> TO <New_Table_Name> ;

Keyword Keyword Current name of the Keyword New name of the


Table Every SQL Query
Table ends with a
semicolon

16
Demo

17
Demo

18
“INSERT INTO” Statement

• Data Manipulation Language (DML) SQL command.


• Used to store data in the table.
• Adds a new record to the table.

• Two ways of using INSERT INTO statement for inserting rows:


1. Only values
2. Column names and values (both)

19
INSERT INTO : Only Values
• Specify only the value of data to be inserted without the
column names.
• Only values : Takes the advantage of the order of the columns
when the table was created.

Syntax:

INSERT INTO Table_Name VALUES ( value1, value2, …. ) ;

Keyword Name of the Table Keyword value of first column,


second column,… Every SQL Query
ends with a
semicolon

20
Example
1
Database Table :
Student_Details

2 INSERT INTO :
only values

Database Table after


3 inserting one row:
Student_Details

21
INSERT INTO :
Column names and Values (both)
• Specify both the columns which we want to fill and their corresponding
values.
• The number of columns and values must be the same.
• If a column is not specified, the default value for the column is used.
• The values specified must satisfy all the applicable constraints such
as primary keys.
• If a syntax error occurs or if any constraints are violated, the new row is not
added to the table and an error is returned instead.

22
INSERT INTO :
Column names and Values (both)
Every SQL Query
Syntax: ends with a
semicolon

( value1, ;
INSERT INTO Table_Name ( column1, VALUES value2,
column2, …. ) …. )
Keyword Name of the Table Keyword

name of first column, value of first column,


second column,… second column,…

23
Example :
INSERT INTO column names and values

Query :

Database Table:
Student_details

24
Database Table : Student_details
1
2
3

Database Table:
Student_details

25
Insert into specific columns in a table

Database Table :
Department

26
Insert multiple rows at a time in a single
SQL statement
name of first column, second
Syntax: Name of the Table column,…

INSERT INTO Table_Name ( columnA, VALUES


columnB, …. )
Keyword Keyword

( value1A, value1B, …. ),
(value2A, value2B, ….), ;
(value3A, value3B, ….)
Every SQL Query
…… ends with a
semicolon
multi-rows record values

27
Database table “Department” with
no records

28
Insert multiple rows at a time into a Table

INSERT INTO

Database table
after inserting

29
“SELECT” Statement
• Data Manipulation Language (DML) SQL command.
• Used to retrieve records from one or more tables.

• The SELECT statement has the following clauses :


▪ DISTINCT: Select distinct rows in a table.
▪ WHERE: Select the rows under the specified conditions.
▪ ORDER BY: Sorts the result according to specified criteria.
▪ HAVING: The conditions under which a category (group) will be included.
▪ GROUP BY: Indicate categorization of results.

30
SELECT all the columns from one table
Syntax : SELECT * FROM Table_Name ;
Keyword “all columns” Keyword Name of the Table Every SQL Query ends with
a semicolon

Example 1: Select all rows in a table

31
SELECT multiple columns from one table
Syntax :
SELECT Column1, column2 ,… FROM Table_Name ;
Keyword Selected columns Keyword Name of the Table Every SQL Query
ends with a
semicolon

Example 2:
Display multiple columns
in a table

32
SELECT Statement:
Filter rows using WHERE clause.
• WHERE clause is used to filter the results from a SELECT, INSERT,
UPDATE, or DELETE statement.

• The general form : WHERE Condition

• where condition is any expression that evaluates to a result of


type Boolean.

• Any row that does not satisfy this condition will be eliminated from the
output.
33
Select Statement with Where Clause

Example 3:

Example 4:

34
Select data based on comparison operator

Example 5:

35
SELECT distinct rows using DISTINCT
operator.
• Used to remove duplicates from the result set.
• The DISTINCT clause can only be used with SELECT statements.

Syntax :

SELECT DISTINCT Column1, FROM Table_Name ;


column2 ,…
Keyword return the Keyword Name of the Table
unique values Every SQL Query
Selected columns ends with a
to be displayed semicolon
in the result set

36
Select Statement with DISTINCT Clause

Example :

Example :

37
SELECT Statement : Sort rows using
ORDER BY clause.

The ORDER BY clause allows you to sort rows returned by a SELECT


clause in ascending or descending order based on a sort expression.

SELECT <column1, column2, …>


FROM <Table_name>
Syntax:
[WHERE condition]
[ORDER BY column1, column2, …] [ASC | DESC];

38
Database Table : Books

39
Examples

Example :

Example :

40
Syntax: UPDATE Statement
• Specify both the columns which we want to fill and
their corresponding values.
Condition to select the rows
for which the values of
Keyword Name of the Table Keyword Keyword columns needs to be updated

UPDATE Table_Name SET Column1= Value1, WHERE <Condition> ;


column2 = Value2,
….
Every SQL
Query ends
Assign Column_names with a
with new value semicolon

41
Database Table : Student_Details

42
Example 1: Update the details of Student_ID = 102

Updated Database Table : Student_Details

43
Example 2: Update the details of Student_ID = 103

Updated Database Table : Student_Details

44
Example 3: Update the already existing data in a record

Updated Database Table : Student_Details

45
Omitting WHERE clause:

Updated Database Table : Student_Details

46
Update Statement: Return Clause
• UPDATE statement returns the following command tag:
• UPDATE count
• Count is the number of rows updated including rows whose values did not
change.
• The UPDATE statement has an optional RETURNING clause that returns the
updated rows.

Syntax:
UPDATE table_name SET column1=value1, column2=value2, ...
WHERE condition RETURNING * | output_expression AS
output_name;

47
Update Statement: Return Clause
Update a row and return the updated row
Example: Modify the published_date of the course to 2020-07-01 and returns
the updated course.
Query: UPDATE course SET published_date = '2020-07-01‘ WHERE
course_id = 2 RETURNING *;
Output Result:

48
DELETE Statement
• Data Manipulation Language (DML) SQL Command.

• Used to delete existing records from a table.

• We can delete a single record or multiple records depending on the


condition we specify in the WHERE clause.
Name of the Table Keyword

DELETE FROM Table_Name WHERE <Condition> ;


Every SQL Query
Keyword Keyword ends with a
Condition to choose a
particular record semicolon

49
Example: Database Table

Table: Employee

50
Example 1: Delete one row in a table

Updated Database Table : Employee

51
Example 2: Delete all rows in a table

Updated Database Table : Employee

52
Delete Statement: Return Clause

• By using the RETURNING clause, you can return the deleted rows to
client as follows:
▪ DELETE FROM Table_Name WHERE condition
RETURNING (select_list | *);
• The asterisk (*) allows you to return all columns of the deleted row.
• To return specific columns, you can specify them after the
RETURNING keyword.
• DELETE statement only removes data from a table.
• It doesn’t modify the structure of the table.
53
Delete Statement: Return Clause

Delete a row and return deleted row


• Example: Deletes the row with id 7 and returns the deleted row to
the client.
▪ Query: DELETE FROM links WHERE id = 7 RETURNING *;

Delete multiple rows from the table


• Example: Delete two rows from the links table and return the
values in the id column of deleted rows.
▪ Query: DELETE FROM links WHERE id IN (6,5) RETURNING *;

54
SQL : Aggregate Functions

• Performs a calculation on a set of values, and returns a single value.

• Except for COUNT(*), aggregate functions ignore null values.

• Often used with the GROUP BY clause of the SELECT statement.

55
Aggregate Functions

• COUNT counts how many rows are in a particular column.

• SUM adds together all the values in a particular column.

• MIN and MAX return the lowest and highest values in a


particular column, respectively.

• AVG calculates the average of a group of selected values.

56
SQL COUNT

Count the number of rows in a particular column.

❑ Syntax : Count all rows


SELECT COUNT(*) FROM <Table_Name>;

❑ Syntax : Count individual columns


SELECT COUNT(<column_name>) FROM <Table_Name>;

57
Database Table
Table : Books

Table :
Employee

58
SQL COUNT

Example 1

Example 2

59
SQL SUM
• Totals the values in a given column.
• Can only use SUM on columns containing numerical values.

Syntax : SELECT SUM(<column_name>) FROM <Table_Name>;

Example 3:

60
SQL MIN & MAX

• MIN and MAX are SQL aggregation functions that return the
lowest and highest values in a particular column.

❑ Syntax :
SELECT MIN(<column_name>) FROM <Table_Name>;

❑ Syntax :
SELECT MAX(<column_name>) FROM <Table_Name>;

61
Example: SQL MIN / MAX

Example 4 :

Example 5 :

62
SQL AVG
• Calculates the average of a selected group of values.
• It's very useful, but has some limitations.
▪ First, it can only be used on numerical columns.
▪ Second, it ignores null values completely.

Syntax : SELECT AVG(<column_name>) FROM <Table_Name>;

Example 6 :

63
Summary
• Discussed SQL Statements.

64
Reference
• Modern database management / Jeffrey A. Hoffer, V. Ramesh,
Heikki Topi. — 10th edition. Pearson Publication.

65
66

You might also like