SQL PPT DDL DML Agg Operator Clauses
SQL PPT DDL DML Agg Operator Clauses
SQL
DBHawk
https://apex.oracle.com/en/learn/getting-started/sql-workshop/
A Logically Related Database
2
Create table Employee_info
(
Emp_ID varchar(30),
Fname varchar(20),
Lname varchar(30),
Desg varchar(35),
sal number(5) );
Desc Employee_info;
Desc employee_data1;
desc employee_data1;
Alter Command:
Rename a Column:
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;
Rename table:
ALTER TABLE old_table_name
RENAME TO new_table_name;
Ex:
ALTER TABLE employees
RENAME TO staff;
desc employee_data1;
Product
PName Price Category Manufacturer
7
Steps to Define the Schema
Step 2: Define Data Types and Domain of Attributes.
Pname : Varchar,
Price: Float,
Category: Varchar
Manfacturer: Varchar
8
Create Table
Schema of Table Product (Example using
Check contraints)
Product(Pname varchar Primary Key,
Price float Not Null,
Category varchar, check(Gadget, Photography,
Household
Manufacturer varchar )
10
Creating a Table
create table product(Pname varchar(20) primary key,
price float NOT NULL,category varchar(20)
CHECK(category
in("Gadget","Photography","Household")),
manufacturer varchar(20));
Attribute Data Type Constraints
Pname Varchar Primary Key
Price Float Not Null
Category Varchar Gadget,
Photography,
Household
Manufacturer Varchar
VIPS: Oct - Dec 2019 11
Show Existing Tables
Show tables;
Desc product;
12
Insert records in Table
INSERT INTO R(A1,…., An) VALUES (v1,…., vn)
or
insert into product values("Gizmo",19.99, "Gadgets", "GizmoWorks");
insert into product values("Powergizmo",29.99, "Gadgets", "GizmoWorks");
insert into product values("SingleTouch",149.99, "Photography", "Canon");
insert into product values("MultiTouch",203.99, "Household", "Hitachi");
SELECT *
FROM product;
14
Select Query using WHERE
PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Product Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
Task: I want to see Pnames MultiTouch 203.99 Household Hitachi
and price from products
table
SELECT Pname, Price
FROM Product PName Price
Gizmo 19.99
Powergizmo 29.99
“projection” SingleTouch 149.99
MultiTouch 203.99
15
Select Query using WHERE
SELECT *
FROM Product
WHERE category=‘Gadgets’;
16
Select Query using WHERE
18
Select Query using WHERE
PName Price Category Manufacturer
Product
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
19
Employee
Practice
Aggregate Functions
Sum
Max
Min
Avg
Count
22
Aggregate Functions – SUM
Sum of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT sum(price)
FROM Product;
403.96
23
Aggregate Functions – MAX
Max of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT max(price)
FROM Product;
203.96
24
Aggregate Functions – MIN
Min of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT min(price)
FROM Product;
19.99
25
Aggregate Functions – AVG
Avg of Price of all Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT avg(price)
FROM Product;
100.99
26
Aggregate Functions – COUNT
Total number of Products
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT count(price)
FROM Product;
4
SELECT count(*)
FROM Product;
27
Eliminating Duplicates
Category
SELECT DISTINCT category
Gadgets
FROM Product;
Photography
Household
Compare to:
Category
Gadgets
SELECT category
FROM Product; Gadgets
Photography
Household
30
More Examples
Query Sql
32
WRITE PName
Gizmo
Price
19.99
Category
Gadgets
Manufacturer
GizmoWorks
THE Powergizmo 29.99 Gadgets GizmoWorks
QUERY SingleTouch
MultiTouch
149.99
203.99
Photography
Household
Canon
Hitachi
33
List of Practicals:
1 Create a student table with the student id, name, and marks as
attributes where the student id is the primary key.
2 Insert details of any 3 students in the student table.
3 Insert the details of 2 new students in the above table
4 Delete the details of a student in the above table.
(Hint: delete from student where student_id=1005; )
5 Use the select command to get the details of the students with marks
more than 80
6 Find the min, max, sum, and average of the marks in a student marks
table
Hint: select max(marks), min(marks), sum(marks) , avg(marks) from
student;
Assignment 1
35
Create a new table in your current database
‘COMPANY’ with the following schema
Stock_Price Float
Country Varchar
36
Create a new table named ‘COMPDTLS’ in your
current database with the following schema
Country Varchar
37
Insert the following Records in COMPDTLS
38
Write SQL Queries for:
1. List the details of all companies
2. List the registration date of all companies
3. Show the details of all companies of Japan
4. List the company name whose stock price is 65
5. List the companies of Japan or India
6. Show the maximum stock price.
7. Show the average stock price.
8. Show the distinct countries
9. Show the total no of countries
10. Show the company name whose country name ends with ‘a’.
39
IN Operator
(Use-Tests whether a value exists in a list of specified values)
40
PName Price Category Manufacturer
Product
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
Write the output of the following SQL Queries: SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
OUTPUT ?
IN/NOT IN Operator
BETWEEN Operator
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE <pattern>
SELECT *
FROM Product
WHERE Pname like ‘P%’;
SELECT *
FROM Product
WHERE Pname like ‘%Touch’;
47
Like Operator with _ &%
Product name with second letter ‘o’
Product PName Price Category Manufacturer
Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
SELECT *
FROM Product
WHERE Pname like ‘_o%’;
48
Like Operator with %
Product name with second last character ‘c’
SELECT *
FROM Product
WHERE Pname like ‘%c_’;
74
FIND PName
Gizmo
Price
19.99
Category
Gadgets
Manufacturer
GizmoWorks
THE Powergizmo 29.99 Gadgets GizmoWorks
RESULT SingleTouch
MultiTouch
149.99
203.99
Photography
Household
Canon
Hitachi