Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Unit – 4

SQL query

Prof. Mittal Darji


Information Technology
GCET, CVMU.
Introduction to DBMS
Section - 1
What is SQL?
 Structured Query Language(SQL) as we all know is the database language by the use of which
we can perform certain operations on the existing database.
 Also, we can use this language to create a database.
 SQL uses certain commands like CREATE, DROP, INSERT, etc. to carry out the required tasks.
 SQL commands are instructions.
 It is used to communicate with the database. It is also used to perform specific tasks,
functions, and queries of data.
 SQL can perform various tasks like creating a table, adding data to tables, dropping the table,
modifying the table, set permission for users.

Prof. Mittal Darji SQL 3


Types of SQL Commands
 There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL
• DDL – Data Definition Language
• DML – Data Manipulation Language
• DQL – Data Query Language
• DCL – Data Control Language
• TCL – Transaction Control Language

Prof. Mittal Darji SQL 4


Types of SQL Commands

Prof. Mittal Darji SQL 5


DDL – Data Definition Language
 DDL creates or changes the structure or schema of the table like creating a table, deleting a
table, altering a table, etc.
 All the command of DDL are auto-committed that means it permanently save all the changes in
the database.
 Here are some commands that come under DDL:
• CREATE
• ALTER
• DROP
• TRUNCATE

Prof. Mittal Darji SQL 6


DDL – Create
 Create is used to create the database or its objects (like table, index, function, views, store
procedure, and triggers).
 Mainly we will use it to create a new table in the database.
 A Table is a combination of rows and columns. For creating a table we have to define the
structure of a table by adding names to columns and providing data type and size of data to be
stored in columns.
 Synatx:
 CREATE TABLE table_name (column_name datatype[size] Constraints, ….);
 Example:
 CREATE TABLE Customer( CustomerID int PRIMARY KEY, CustomerName VARCHAR(50),
LastName VARCHAR(50), Country VARCHAR(50), Age int(2), Phone int(10) );
 CREATE TABLE employee(name varchar2(20), email varchar2(100), dob date);

Prof. Mittal Darji SQL 7


DDL – Drop
 It is used to delete both the structure and record stored in the table.

 Syntax:
 DROP TABLE table_name;

 Example:
 DROP TABLE EMPLOYEE;

Prof. Mittal Darji SQL 8


DDL – Alter
 It is used to alter the structure of the database.
 It is used to add, remove, or modify columns in an existing table.
 The ALTER TABLE statement is also used to add and remove various constraints on existing
tables.
 To add a new column in the table:
 Syntax:
 ALTER TABLE table_name ADD column_name COLUMN-definition;
 Example:
 ALTER TABLE stu_details ADD(address varchar2(20));

Prof. Mittal Darji SQL 9


DDL – Alter
 To modify the existing columns in a table.
 Multiple columns can also be modified at once.
 Syntax:
 ALTER TABLE table_name MODIFY(column_definitions....);
 Example:
 ALTER TABLE stu_details MODIFY (name varchar2(20));

Prof. Mittal Darji SQL 10


DDL – Alter
 DROP COLUMN is used to drop columns in a table. Deleting the unwanted columns from the
table.
 Multiple columns can also be modified at once.
 Syntax:
 ALTER TABLE table_name DROP COLUMN column_name;

 Example:
 ALTER TABLE Students DROP COLUMN Email;

Prof. Mittal Darji SQL 11


DDL – Truncate
 This is used to remove all records from a table, including all spaces allocated for the records
are removed
 Syntax:
 TRUNCATE TABLE table_name;

 Example:
 TRUNCATE TABLE employee;

Prof. Mittal Darji SQL 12


Difference between DROP and TRUNCATE

Prof. Mittal Darji SQL 13


Data Manipulation Language - DML
 DML commands are used to modify the database.
 It is responsible for all form of changes in the database.
 The command of DML is not auto-committed that means it can't permanently save all the
changes in the database. They can be rollback.
 Here are some commands that come under DML:
• INSERT
• UPDATE
• DELETE

Prof. Mittal Darji SQL 14


DML - Insert
 The INSERT INTO statement of SQL is used to insert a new row/record in a table.

 Syntax:
1. INSERT INTO table_name VALUES (value1, value2, value3);
2. INSERT INTO table_name (column1, column2, column3) VALUES ( value1, value2, value3);

 Example:
1. INSERT INTO Student VALUES ('5','harsh','west bengal','xxxxxxxxxx','19');
2. INSERT INTO Student (Roll_no, Name, age) VALUES ('5','pratik','19');
 The fields for which values are not provided will get null.

Prof. Mittal Darji SQL 15


DML - Update
 This command is used to update the data of an existing table in the database.
 We can update single columns as well as multiple columns using the UPDATE statement as per
our requirement.
 Syntax:
 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;

 Example:
1. UPDATE Customer SET CustomerName = 'Nitin' WHERE Age = 22;
2. UPDATE Customer SET CustomerName = 'Satyam', Country = 'USA' WHERE CustomerID = 1;
3. UPDATE Customer SET CustomerName = 'Shubham';

Prof. Mittal Darji SQL 16


DML - Delete
 Existing records in a table can be deleted using the SQL DELETE Statement.
 We can delete a single record or multiple records depending on the condition we specify in the
WHERE clause.
 Syntax:
 DELETE FROM table_name WHERE some_condition;

 Example:
1. DELETE FROM employees WHERE name = 'Rithvik';
 Delete all the records:
1. DELETE FROM employees;
2. DELETE * FROM employees;

Prof. Mittal Darji SQL 17


Data Query Language - DQL
 DQL is used to fetch the data from the database.
 It uses only one command:
• SELECT

Prof. Mittal Darji SQL 18


DQL - Select
 This is the same as the projection operation of relational algebra.
 It is used to select the attribute based on the condition described by WHERE clause.
 Syntax:
 SELECT column1,column2 FROM table_name;
 SELECT * FROM table_name;
 SELECT * FROM table_name WHERE condition;

 Example:
 SELECT * FROM Customer;
 SELECT CustomerName FROM Customer where Age = '21';

Prof. Mittal Darji SQL 19


DQL - Select

Prof. Mittal Darji SQL 20


Order by
 SELECT * FROM Customers ORDER BY Country;
 SELECT * FROM Customers ORDER BY Country DESC;

Prof. Mittal Darji SQL 21


AND & OR
 The AND operator displays a record if all the conditions are TRUE.
 The OR operator displays a record if any of the conditions are TRUE.

 SELECT * FROM Customers WHERE Country = 'Germany‘ AND City = 'Berlin‘ AND PostalCode >
12000;
 SELECT * FROM Customers WHERE Country = 'Germany‘ OR Country = India‘;

Prof. Mittal Darji SQL 22


NOT
 SELECT * FROM Customers WHERE NOT CustomerID > 50;
 SELECT * FROM Customers WHERE CustomerID NOT BETWEEN 10 AND 60;
 SELECT * FROM Customers WHERE NOT Country = 'Spain';
 SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NULL;

Prof. Mittal Darji SQL 23


Null
 SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NULL;
 SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT
NULL;

Prof. Mittal Darji SQL 24


Like
 The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

Prof. Mittal Darji SQL 25


Like
 Select all customers that starts with the letter "a":
 SELECT * FROM Customers WHERE CustomerName LIKE 'a%';

 Return all customers from a city that contains the letter 'L':
 SELECT * FROM Customers WHERE city LIKE '%L%';

 Return all customers that starts with 'La':


 SELECT * FROM Customers WHERE CustomerName LIKE 'La%';

 Return all customers that starts with 'a' or starts with 'b':
 SELECT * FROM Customers WHERE CustomerName LIKE 'a%' OR CustomerName LIKE 'b%';

Prof. Mittal Darji SQL 26


Like
 Return all customers that ends with 'a':
 SELECT * FROM Customers WHERE CustomerName LIKE '%a';

 Return all customers that starts with "b" and ends with "s":
 SELECT * FROM Customers WHERE CustomerName LIKE 'b%s';

 Return all customers that starts with "a" and are at least 3 characters in length:
 SELECT * FROM Customers WHERE CustomerName LIKE 'a__%';

 Return all customers that have "r" in the second position:


 SELECT * FROM Customers WHERE CustomerName LIKE '_r%';

Prof. Mittal Darji SQL 27


Like
 Return all customers from Spain:
 SELECT * FROM Customers WHERE Country LIKE 'Spain';

 Return all customers starting with either "b", "s", or "p":


 SELECT * FROM Customers WHERE CustomerName LIKE '[bsp]%';

 Return all customers starting with "a", "b", "c", "d", "e" or "f":
 SELECT * FROM Customers WHERE CustomerName LIKE '[a-f]%';

Prof. Mittal Darji SQL 28


Alias - AS
 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.

Prof. Mittal Darji SQL 29


Alias - AS
 SELECT CustomerID AS ID, CustomerName AS Customer FROM Customers;

 SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address FROM


Customers;

Prof. Mittal Darji SQL 30


Aggregate functions – min max
 The MIN() function returns the smallest value of the selected column.

 The MAX() function returns the largest value of the selected column.

Prof. Mittal Darji SQL 31


Aggregate functions
 SELECT MIN(Price) FROM Products;

 SELECT MAX(Price) FROM Products;

 SELECT MIN(Price) AS SmallestPrice FROM Products;

Prof. Mittal Darji SQL 32


Aggregate functions - Count
 The COUNT() function returns the number of rows that matches a specified criterion.

 Find the number of products where Price is higher than 20:


 SELECT COUNT(ProductID) FROM Products WHERE Price > 20;

 Find the number of products where the ProductName is not null:


 SELECT COUNT(ProductName) FROM Products;

Prof. Mittal Darji SQL 33


Aggregate functions - SUM
 The SUM() function returns the total sum of a numeric column.

 Return the sum of all Quantity fields in the OrderDetails table:


 SELECT SUM(Quantity) FROM OrderDetails;

 SELECT SUM(Quantity) AS total FROM OrderDetails WHERE ProdictId = 11;

 SELECT SUM(Price * Quantity) FROM OrderDetails;


 SELECT SUM(Quantity * 10) FROM OrderDetails;

Prof. Mittal Darji SQL 34


Aggregate functions - AVG
 The AVG() function returns the average value of a numeric column.
 NULL values are ignored.

 Find the average price of all products:


 SELECT AVG(Price) FROM Products;

 SELECT AVG(Price) AS [average price] FROM Products WHERE CategoryID = 1;

 Return all products with a higher price than the average price:
 SELECT * FROM Products WHERE price > (SELECT AVG(price) FROM Products);

Prof. Mittal Darji SQL 35

You might also like