Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

SQL PPT DDL DML Agg Operator Clauses

ppt on sql

Uploaded by

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

SQL PPT DDL DML Agg Operator Clauses

ppt on sql

Uploaded by

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

Data analytics using SQL

Course Code: UDT101

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;

Alter Table Employee_info rename to employee_data1;

Desc employee_data1;

Alter table employee_data1


Add Address varchar(100);

Alter table employee_data1 modify Lname varchar(20);

Alter table employee_data1drop column Address;

desc employee_data1;
Alter Command:

Add a Column to an Existing Table:


ALTER TABLE table_name
ADD column_name data_type;

Ex: ALTER TABLE employees


ADD email VARCHAR2(100);

Modify an Existing Column:


ALTER TABLE table_name
MODIFY column_name new_data_type;

Ex: ALTER TABLE employees


MODIFY salary NUMBER(10, 2);

Rename a Column:
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

Ex: ALTER TABLE employees


RENAME COLUMN emp_name TO employee_name;
Drop a Column:
ALTER TABLE table_name
DROP COLUMN column_name;

Ex: ALTER TABLE employees


DROP COLUMN email;

Add or Modify Constraints:


To modify constraint
ALTER TABLE employees
ADD CONSTRAINT pk_employee_id PRIMARY KEY (employee_id);

Rename table:
ALTER TABLE old_table_name
RENAME TO new_table_name;

Ex:
ALTER TABLE employees
RENAME TO staff;
desc employee_data1;

Insert into employee_data1values('110012','Sana','Khan','Clerk',20000);

Insert into employee_data1values('110013','jeba','Khan','Manager',90000);

Insert into employee_data1values('110014','Jiya','Bansal','Accountant',50000);

select * from employee_data1;

Delete duplicate row from Table


create table emp_data as
select distinct * from employee_data1;
Steps to Define the Schema
Step 1: Define table name and its attributes

Product(PName, Price, Category, Manufacturer)

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

7
Steps to Define the Schema
Step 2: Define Data Types and Domain of Attributes.

Product(PName, Price, Category, Manfacturer)

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 )

Attribute Data Type Constraints


Pname Varchar Primary Key
Price Float Not Null
Category Varchar 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;

Describe structure of a Existing Table


Desc <tablename>;

Desc product;
12
Insert records in Table
INSERT INTO R(A1,…., An) VALUES (v1,…., vn)

insert into product(Pname,price,category,manufacturer) values("Gizmo",19.99, "Gadgets",


"GizmoWorks");

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");

PName Price Category Manufacturer


Gizmo 19.99 Gadgets GizmoWorks

Powergizmo 29.99 Gadgets GizmoWorks

SingleTouch 149.99 Photography Canon

MultiTouch 203.99 Household Hitachi


13
Select Query
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>

Task: To display the complete product Table

SELECT *
FROM product;

PName Price Category Manufacturer


“selection”
Gizmo 19.99 Gadgets GizmoWorks

Powergizmo 29.99 Gadgets GizmoWorks

SingleTouch 149.99 Photography Canon

MultiTouch 203.99 Household Hitachi

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

Product PName Price Category Manufacturer


Gizmo 19.99 Gadgets GizmoWorks
Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon
Find products details of gadgets category
MultiTouch 203.99 Household Hitachi

SELECT *
FROM Product
WHERE category=‘Gadgets’;

PName Price Category Manufacturer


“selection” with Gizmo 19.99 Gadgets GizmoWorks
where
Powergizmo 29.99 Gadgets GizmoWorks

16
Select Query using WHERE

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 PName, Price, Manufacturer


FROM Product
WHERE Price > 100;

PName Price Manufacturer


SingleTouch 149.99 Canon
“selection” and
“projection” with MultiTouch 203.99 Hitachi
where
17
Select Query using WHERE

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 PName, Price, Manufacturer


FROM Product
WHERE Price > 100 and manufacturer =“Canon”;

Combine two or more


PName Price Manufacturer
conditions Using
SingleTouch 149.99 Canon
and

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

SELECT PName, Price, Manufacturer


FROM Product
WHERE manufacturer =“Hitachi” or
manufacturer = “Canon”;

Combine two or more PName Price Manufacturer


conditions Using SingleTouch 149.99 Canon
or MultiTouch 203.99 Hitachi

19
Employee
Practice
Aggregate Functions

SQL supports several aggregation operations:

 Sum
 Max
 Min
 Avg
 Count

Except count, all aggregations apply to a single attribute

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

Max price of Gadgets Select Max(price) from


category Products product where
category=“Gadgets”

Total no of products in Select count(*) from product


Household category where Category=“Household”

Count total no. of categories Select


Count(Distinct(category) )
from product

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

Problem Statement SQL Query


Average Price of Gizmo Works
manufacturer ?
Total price of Gizmo Works manufacturer
?
Count total number of manufacturers
?
Count number of products that contains ‘o’
in their name
?

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

Attribute Data Type Constraints

Cname Varchar Primary Key

Reg_Date Date Not Null

Stock_Price Float

Country Varchar

36
Create a new table named ‘COMPDTLS’ in your
current database with the following schema

Attribute Data Type Constraints


CompName Varchar Primary Key
RegDate Date Not Null
StockPrice Float

Country Varchar

COMPDTLS( CompName varchar Primary Key,


RegDate Date Not Null,
StockPrice Float
Country varchar )

37
Insert the following Records in COMPDTLS

CompName RegDate StockPrice Country

GizmoWorks 2019/10/21 25 USA

Canon 2019/10/3 65 Japan

Hitachi 2019/10/10 15 India

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)

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

SELECT PName, Price, Manufacturer


FROM Product
WHERE manufacturer IN(‘Hitachi’, ’Canon’);

Replace OR with In PName Price Manufacturer


conditions Using SingleTouch 149.99 Canon
IN MultiTouch 203.99 Hitachi

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

SELECT * FROM products WHERE category IN (‘Gadgets’, ’Photography’);

OUTPUT ?
IN/NOT IN Operator
BETWEEN Operator
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE <pattern>

Pattern : pattern matching on strings. It contains two


special symbols:
% = any sequence of characters (matches any substring)
_ = any single character (matches any character)

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
44
Like Operator with %
Product name that starts with P

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 ‘P%’;

PName Price Category Manufacturer

Powergizmo 29.99 Gadgets GizmoWorks


45
Like Operator with %
Product name that ends with Touch

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 ‘%Touch’;

Look for substring ending with Touch


PName Price Category Manufacturer
SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
46
Like Operator with %
Product name that contains e anywhere in the name

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 ‘%e%’;

PName Price Category Manufacturer


Powergizmo 29.99 Gadgets GizmoWorks
SingleTouch 149.99 Photography Canon

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%’;

Name start with ‘any


character followed by PName Price Category Manufacturer
o’
Powergizmo 29.99 Gadgets GizmoWorks

48
Like Operator with %
Product name with second last character ‘c’

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 ‘%c_’;

PName Price Category Manufacturer


SingleTouch 149.99 Photography Canon
MultiTouch 203.99 Household Hitachi
49
Write the output of the following Queries

Select * from Employee


Where ename like ‘_ _ _ _ _’;

(no space between underscore)


Answers

Displays employee name whose name has five characters.


Null Operator
Airthmatic Arithmatic Operators
Clauses
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 order by Pname;

Select * from product order by Pname desc;


Write Output
Example
Queries and Subqueries
Nested Query
Ordering the Results
SELECT pname, price, manufacturer
FROM Product
WHERE manufacturer=‘GizmoWorks’ AND price > 50
ORDER BY price, pname;

• Ties are broken by the second attribute on the ORDER


BY list, etc.

• Also works without Where


• Ordering is ascending, unless you specify the DESC
keyword.

SELECT pname, price, manufacturer


FROM Product
ORDER BY price DESC;

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

SELECT DISTINCT category


FROM Product
ORDER BY category ?
SELECT Category
FROM Product
ORDER BY PName ?
SELECT DISTINCT category
FROM Product
ORDER BY PName ? 75

You might also like