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.
SQL is a standard language for accessing and manipulating databases.
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.
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.
What are SQL Commands?
SQL commands are the fundamental building blocks for communicating with a
database management system (DBMS). These commands perform various database
operations, such as creating tables, inserting data, querying information, and
controlling access and security. SQL commands can be categorized into different
types, each serving a specific purpose in the database management process.
Categorization of SQL Commands
SQL commands can be categorized into five primary types, each serving a distinct
purpose in database management. Understanding these categories is essential for
efficient and effective database operations. SQL commands can be categorized into
four main types:
Types of SQL Commands:
DDL (Data Definition Language):
CREATE: Creates a new table or database.
ALTER: Modifies an existing database object.
DROP: Deletes an entire table, database, or other objects.
TRUNCATE: Removes all records from a table, deleting the space allocated for the
records.
DML (Data Manipulation Language):
SELECT: Retrieves data from the database.
INSERT: Adds new data to a table.
UPDATE: Modifies existing data within a table.
DELETE: Removes data from a table.
DCL (Data Control Language):
GRANT: Gives users access privileges to the database.
REVOKE: Removes access privileges given with the GRANT command.
TCL (Transaction Control Language):
COMMIT: Saves all changes made in the current transaction.
ROLLBACK: Restores the database to the last committed state.
SAVEPOINT: Sets a savepoint within a transaction.
What is DDL? Data Definition Language (DDL) Commands
DDL, which stands for Data Definition Language, is a subset of SQL (Structured Query
Language) commands used to define and modify the database structure. These
commands are used to create, alter, and delete database objects like tables, indexes,
and schemas.
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,
....
);
SQL CREATE TABLE Example
The following example creates a table called "persons" that contains five columns:
PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons (
PersonID int,
LastName varchar (255),
FirstName varchar (255),
Address varchar (255),
City varchar (255)
);
SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
ALTER TABLE - ADD Column
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that some database
systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE - RENAME COLUMN
To rename a column in a table, use the following syntax:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
ALTER TABLE - ALTER/MODIFY DATATYPE
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
The SQL TRUNCATE TABLE Statement
The SQL TRUNCATE TABLE command is used to empty a table. This command is a
sequence of DROP TABLE and CREATE TABLE statements and requires the DROP
privilege.
Syntax
The basic syntax of a TRUNCATE TABLE command is as follows.
TRUNCATE TABLE table_name;
Experiment 2
The structured query language (SQL) commands deal with the manipulation of data
present in the database that belongs to the DML or Data Manipulation Language. This
includes most of the SQL statements.
1. 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.
Select ALL columns
If you want to return all columns, without specifying every column name, you can use
the SELECT * syntax:
SQL SELECT DISTINCT Statement
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
2. SQL INSERT INTO Statement
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two ways:
Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
3 The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
4.SQL DELETE Statement
The SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
DELETE FROM table_name WHERE condition;
SQL WHERE Clause
The SQL WHERE Clause
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Experiment 4
SQL Aggregate Functions
An aggregate function is a function that performs a calculation on a set of values, and
returns a single value.
Aggregate functions are often used with the GROUP BY clause of the SELECT
statement. The GROUP BY clause splits the result-set into groups of values and the
aggregate function can be used to return a single value for each group.
The most commonly used SQL aggregate functions are:
MIN() - returns the smallest value within the selected column
MAX() - returns the largest value within the selected column
COUNT() - returns the number of rows in a set
SUM() - returns the total sum of a numerical column
AVG() - returns the average value of a numerical column
SQL MIN() and MAX() Functions
Find the highest price in the Price column:
select max(price)from product50;
Find the lowest price in the Price column:
select min(price)from product50;
The SQL COUNT() Function
The COUNT() function returns the number of rows that matches a specified criterion.
select count(*) from product50;
SQL SUM() Function
The SUM() function returns the total sum of a numeric column.
select sum(price) from product50;
The SQL AVG() Function
The AVG() function returns the average value of a numeric column.
SELECT AVG(Price)
FROM Product50;
Experiment 6
SQL Arithmetic Operators
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulo