0% found this document useful (0 votes)
34 views

Structured Query Language (SQL)

The document discusses the different components of Structured Query Language (SQL). It describes the main datatypes in SQL like char, varchar, int, and numeric. It then explains the four main components of SQL - data definition language, data manipulation language, data query language, and data control language. For each component, it provides examples of common commands like CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE, GRANT and REVOKE. It also covers topics like aggregation functions, group functions, subqueries and set operators.

Uploaded by

skatedude_987
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Structured Query Language (SQL)

The document discusses the different components of Structured Query Language (SQL). It describes the main datatypes in SQL like char, varchar, int, and numeric. It then explains the four main components of SQL - data definition language, data manipulation language, data query language, and data control language. For each component, it provides examples of common commands like CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE, GRANT and REVOKE. It also covers topics like aggregation functions, group functions, subqueries and set operators.

Uploaded by

skatedude_987
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

STRUCTURED QUERY LANGUAGE (SQL)

NEHA J MENDJOGE

DATATYPES IN SQL

char(n). Fixed length character string, with userspecified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). numeric(p,d). Fixed point number, with userspecified precision of p digits, with d digits to the right of decimal point. number(n)
2

NEHA J MENDJOGE

DATA DEFINITION LANGUAGE


NEHA J MENDJOGE

DATA MANIPULATION LANGUAGE

DATA QUERY LANGUAGE


DATA CONTROL LANGUAGE

1. DATA DEFINITION LANGUAGE


Provides commands for creating schemas, deleting relations and modifying relations. Changes made are permanent

NEHA J MENDJOGE

CREATE ALTER DROP TRUNCATE RENAME

CREATE
An SQL relation is defined using the create table command: create table r (A1 D1, A2 D2, ..., An Dn) Where A1 = attribute name D1 = datatype

NEHA J MENDJOGE

Example: Branch(bid, bname, addr) Create table branch ( Bid int, Bname varchar(10), Addr varchar(20) )

ALTER
ALTER TABLE < table name > ADD (<new column name><data type> (<size>) ; <New column name><data type> (<size>);

NEHA J MENDJOGE

Eg: Alter table branch Add ( telno number (8));

DROP ALTER TABLE <table name> DROP COLUMN <column name>; Eg: Alter table branch Drop column telno; Drop table branch;

NEHA J MENDJOGE

TRUNCATE Truncate table <tablename>


NEHA J MENDJOGE

2. DATA MANIPULATION LANGUAGE

INSERT

UPDATE UPDATE <table name> SET <column name>= <expression >, <Column name >=<expression >;

NEHA J MENDJOGE

DELETE DELETE FROM <table name>;

10

3. DATA QUERY LANGUAGE SELECT

NEHA J MENDJOGE

4. DATA CONTROL LANGUAGE GRANT REVOKE

11

BASIC STRUCTURE

A typical SQL query has the form:


NEHA J MENDJOGE

select A1, A2, ..., An from r1, r2, ..., rm where P


Ais represent attributes ris represent relations P is a predicate.

12

THE SELECT CLAUSE

The select clause list the attributes desired in the result of a query

E.g. find the names of all branches in the loan relation select branch-name from loan
Find the names of all branches in the loan relations, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates not be removed. select all branch-name from loan

NEHA J MENDJOGE

13

An asterisk in the select clause denotes all attributes select * from loan The query: select loan-number, branch-name, amount 100 from loan would return a relation which is the same as the loan relations, except that the attribute amount is multiplied by 100.

14

NEHA J MENDJOGE

THE WHERE CLAUSE

The where clause specifies conditions that the result must satisfy
NEHA J MENDJOGE

To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name = Perryridge and amount > 1200
15

THE RENAME OPERATION


The SQL allows renaming relations and attributes using the as clause: old-name as new-name Eg: select customer-name as NAME from borrower

NEHA J MENDJOGE

16

CONSIDER THE FOLLOWING DATABASE:


Student( sno, sname, city, stel, col)
NEHA J MENDJOGE

Course(cno, type, subfee, duration)

Prof(pno, pname, city, sal)


Enroll(sno, cno, doe, bal) Teach(cno, pno)
17

NEHA J MENDJOGE

SET OPERATORS
18

THE UNION OPERATOR


A B

The UNION operator returns results from both queries after eliminating duplications.

THE UNION ALL OPERATOR


A B

The UNION ALL operator returns results from both queries, including all duplications.

THE INTERSECT OPERATOR


A B

THE MINUS OPERATOR


A B

SUMMARY OF SET OPERATORS

Use UNION to return all distinct rows Use UNION ALL to returns all rows, including duplicates Use INTERSECT to return all rows shared by both queries Use MINUS to return all distinct rows selected by the first query but not by the second

27

NEHA J MENDJOGE

AGGREGATE FUNCTIONS

These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

28

NEHA J MENDJOGE

WHAT ARE GROUP FUNCTIONS?


Group functions operate on sets of rows to give one result per group. EMPLOYEES

The maximum salary in the EMPLOYEES table.

CREATING GROUPS OF DATA


EMPLOYEES
4400 9500

The average 3500 salary in EMPLOYEES 6400 table for each department.
10033

All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.

USING THE GROUP BY CLAUSE

SELECT department_id, AVG(salary) FROM employees GROUP BY department_id ;

GROUPING BY MORE THAN ONE COLUMN


EMPLOYEES

Add up the salaries in the EMPLOYEES table for each job, grouped by department.

branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)
33
NEHA J MENDJOGE

SUBQUERIES
Employee(eid, lname, jobid, commission, hire_date, did,salary) Department (did, dname, location_id) Location (location_id, city)

34

NEHA J MENDJOGE

CONSIDER THE FOLLOWING DATABASE:


Student( sno, sname, city, stel, col)
NEHA J MENDJOGE

Course(cno, type, subfee, duration)

Prof(pno, pname, city, sal)


Enroll(sno, cno, doe, bal) Teach(cno, pno)
35

CONSIDER THE FOLLOWING DATABASE:


Employee(eno,ename,esal,ephone,city,position)
NEHA J MENDJOGE

Allotted(eno,pno,date,task)

Project(pno,location,type,deadline,budget)

36

You might also like