Chapter1_OverviewOfBasicSQLStatement_1
Chapter1_OverviewOfBasicSQLStatement_1
(Elective)
Advance Database using PL/SQL
Course Contents
1. Overview of Basic SQL statement
2. Overview to PL/SQL
3. Performing SQL Operations from PL/SQL
4. Using PL/SQL Subprogram
5. Using PL/SQL Package
6. Database Triggers
7. Handling PL/SQL Errors
Tools
1. Ms SQL Server 2012 or Higher
2. SSMS (SQL Server Management Studio)
• Download Link
• (For SQL server 2018)
• https://www.microsoft.com/en-us/sql-server/sql-server-downloads
SQL Commands
• SQL commands are instructions. It is used to communicate with the database.
• We use suitable SQL commands to extract and modify information of the
database.
• There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
DROP : It is used to delete both the structure and record stored in the table.
Syntax : DROP TABLE table_name;
Example : DROP TABLE EMPLOYEE;
UPDATE: This command is used to update or modify the value of a column in the table.
Syntax : UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]
Example : UPDATE EMPLOYEE SET name = ‘Shyam' WHERE name = ‘ram‘
Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example : DELETE FROM CUSTOMERS WHERE AGE = 25; COMMIT;
Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax : ROLLBACK;
Example : DELETE FROM CUSTOMERS WHERE AGE = 25; ROLLBACK;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Select Statement
The SELECT statement is used to select data
from a database.
Apart from information retrieval, this statement also gives query capability. It means that when
select command runs the information present in table will be displayed on the screen.
Syntax: The three common elements of SELECT command is SELECT, FROM and WHERE. These
elements can retrieve information from more than one table. The syntax is:
SELECT <column_list>
FROM <table_list>
WHERE <search_criteria>
Where,
<column_list> defines the list attributes whose value is to be extracted
<table list> defines the list of relation names
<Condition> defines the conditional expression that recognizes the tuple.
Example : SELECT CustomerName, City FROM Customers;
** Tuple: Tuple is a term which refers to a collection of one or more attributes
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
In SQL, basic logical comparison operators are used on the WHERE clause.
Comparison operators and their meanings are
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Select Statement-Example’s
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Select Statement-Example’s
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT * FROM Users WHERE status = 'Single';
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT * FROM Users WHERE age > 30
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT Using the AND Operator
SELECT *FROM Users WHERE status = 'Single' AND age > 30
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT Using the BETWEEN Operator
SELECT * FROM Users WHERE age BETWEEN 20 AND 30
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SELECT Using the IN Operator
SELECT * FROM Users WHERE age IN (56,33,10);
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
SQL Select ORDER BY Clause
SELECT * FROM Users ORDER BY first_name ASC;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Display the employees whose DEPTNO is the same as that of employee 1821 :
Select ENAME, DEPTNO FROM EMP Where DEPTNO = (SELECT DEPTNO FROM EMP WHERE EMPNO =
1821);
** In the example above you can see that the inner query is executed firstly and then the result is followed by the
outer query.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Example: Write a query to List the number of employee in the company from a table employee
SELECT COUNT (*) FROM EMPLOYEE
(b) SUM: SUM function is written with column name and gives the sum of all tupples present in that column.
(c) AVG: AVG function or Average function is written with column name and returns the AVG value of that
column.
(d) MAX: MAX function or Maximum value function written with column name returns the maximum value
present in that column.
(e) MIN: MIN function or Minimum value function written with column name returns the minimum value
present in that column.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Find the sum of salaries of all the employees and also the minimum, maximum and average
salary.
Solution:
SELECT SUM (E.ESAL) AS SUM_SALARY,
MAX (E.ESAL) AS MAX_SALARY,
MIN (E.ESAL) AS MIN_SALARY,
AVG ([DISTINCT] E.ESAL) AS AVERAGE_SALARY
FROM EMPLOYEE
This query calculates the total, minimum, maximum and average salaries
and also renames the column names.
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Syntax :
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3,...)
Or
INSERT INTO table_name VALUES (value1, value2, value3, ...)
Example :
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway')
Insert Multiple Rows
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country) VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Syntax :
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example :
UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
SQL JOINS
The SQL JOIN joins two tables based on a common column and selects records that have matching values in
these columns.
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
INNER JOIN
INNER JOIN selects records that have matching
values in both tables as long as the condition
is satisfied.
It returns the combination of all rows from both the
tables where the condition satisfies.
Example :
-- join the Customers and Orders tables with matching fields
customer_id and customer
Example :
-- left join the Customers and Orders tables
Example :
-- full join Customers and Orders tables
-- based on their shared customer_id columns
-- Customers is the left table
-- Orders is the right table
Views
A view is a subschema in which logical tables are generated from more than one base table.
View is stored as a query as it does not contain its own data. During the query execution contents are taken from
other tables.
When the table content gets modified or changed then the view will change dynamically
Syntax :
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Advance Database using PL/SQL
Unit 1: Overview of Basic SQL statement
Views Example
CREATE VIEW employee_view AS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department = 'IT';
Using a View:
SELECT * FROM employee_view;
Modifying a View:
Views Example
CREATE VIEW vwEmployeesByDepartment
AS
SELECT emp.ID,
emp.Name,
emp.Salary,
CAST(emp.DOB AS Date) AS DOB,
emp.Gender,
dept.Name AS DepartmentName
FROM Employee emp
INNER JOIN Department dept
Now execute the following query which will return the data as expected. ON emp.DeptID = dept.ID
SELECT * FROM vwEmployeesByDepartment