Вы находитесь на странице: 1из 28

[CS-211] SQL

Fall 2016
Objective

Definition of terms
Role of SQL
Write single table queries using SQL
Establish referential integrity using
SQL
What is SQL?

Structured Query Language


SQL is an English-like language
SQL Commands are not Case
Sensitive
When a user wants to get some
information from a database file, he
can issue a query.
A query is a userrequest to retrieve
data or information with a certain
condition.
SQL Environment
Data Definition Language (DDL)
Commands that define a database,
including creating, altering, and
dropping tables and establishing
constraints
Data Manipulation Language (DML)
Commands that maintain and query a
database
Data Control Language (DCL)
Commands that control a database,
including administering privileges and
Schematic of Typical SQL
Environment
DDL, DML, DCL
Retrieval Queries In SQL

Used for queries on single or multiple


tables
Clauses of the SELECT statement:
SELECT
List the columns (and expressions) that should be returned
from the query
FROM
Indicate the table(s) or view(s) from which data will be
obtained
WHERE
Indicate the conditions under which a row will be included in
the result
GROUP BY
Indicate categorization of results
HAVING
Indicate the conditions under which a category (group) will
be included
Relational Database
schema
EMPLOYEE(EMPID, EMPNAME, JOB, MGR,
HIREDATE, SAL, COMM, DEPTNO)

DEPARTMENT(DEPTNO, DNAME, LOC)

SALARY-GRADE(GRADE, LOSAL, HISAL)


Simple Queries
Q0: SELECT EMPID
FROM EMPLOYEE;

Q1: SELECT EMPID, EMPNAME


FROM EMPLOYEE;

Q2: SELECT *
FROM EMPLOYEE;

SELECT Statement With WHERE Clause

Syntax: SELECT Columns FROM Table WHERE Condition.


Q3. SELECT ENAME, JOB FROM EMPLOYEE WHERE SAL < 275;
Simple Queries

SELECT Statement with ORDER BY Clause


ASC, DESC
SELECT * FROM EMPLOYEE
ORDER BY ENAME;
SELECT ENAME, JOB, SAL FROM EMPLOYEE
ORDER BY SAL DESC; Operato Symbol
Arithmetic Operators r
Logical Operators + Add
AND, OR, NOT - Subtra
Between Operator ct
Between (2000 and 5000)
* Multipl
SELECT Example
Using a Function
Using the COUNT aggregate function to find
totals
To Find Sum, Averages, Count, Max, Min Values
AVG => Calculate the average value in a column
COUNT => Determines the number of rows
MAX => Maximum value in a Column
MIN => Minimum value in a Column
SUM => Total of the value in the column

SELECT COUNT(*) FROM ORDER_LINE_V


WHERE ORDER_ID = 1006;

Note: with aggregate functions you cant have single-valued


columns included in the SELECT clause
SELECT ExampleBoolean
Operators
AND, OR, and NOT Operators for customizing
conditions in WHERE clause

SELECT PRODUCT_DESCRIPTION,
PRODUCT_FINISH, STANDARD_PRICE
FROM PRODUCT_V
WHERE (PRODUCT_DESCRIPTION LIKE %Desk
OR PRODUCT_DESCRIPTION LIKE %Table)
AND UNIT_PRICE > 300;
https://
blog.hubspot.com/marketing/sql-tutorial-introduction#s
m.00000nmffa7gjkdr6vt8f2ik2wr1y
Wildcard Character
SELECT Like Operator
In Some Cases Exact Matches not work
Use the Like Operator with Wild Card
SELECT PRODUCT_DESCRIPTION,
PRODUCT_FINISH, STANDARD_PRICE
FROM PRODUCT_V
WHERE (PRODUCT_DESCRIPTION LIKE %Desk
OR PRODUCT_DESCRIPTION LIKE %Table)
AND UNIT_PRICE > 300;

Note: the LIKE operator allows you to compare strings using


wildcards. For example, the % wildcard in %Desk indicates that
all strings that have any number of characters preceding the
word Desk will be allowed
SELECT Operators:
Sorting
Data is not stored in some define order.
ROWS displays in the Order how data store
in the database
Order By, DESC, ASC
SELECT Customer_ID, Customer_name, Ordered
quantity
FROM Customer
Order By Ordered_Quantity
Desc,Customer_Name;
Nesting Queries
IN Operator

SELECT ORDER_NUM FROM ORDER_LINE


WHERE
PART_NUM IN (1,2,3,5,10);

SELECT ORDER_ID FROM ORDER_LINE


WHERE
PRODUCT_ID IN (SELECT PRODUCT_ID
FROM ORDER_LINE
WHERE UNIT_PRICE >=500);
Group By Clause
Categorizing Results Using the GROUP BY Clause
For use with aggregate functions
Scalar aggregate: single value returned from SQL query with
aggregate function
Vector aggregate: multiple values returned from SQL query
with aggregate function (via GROUP BY)

SELECT CUSTOMER_STATE
FROM CUSTOMER_V
GROUP BY CUSTOMER_STATE;

Note: you can use single-value fields with aggregate


functions if they are included in the GROUP BY clause
Insert Statement
Adds data to a table
Inserting into a table
INSERT INTO CUSTOMER_T
VALUES (001, Contemporary Casuals, 1355 S.
Himes Blvd., Gainesville, FL, 32601);
Inserting a record that has some null
attributes requires identifying the fields
that actually get data
INSERT INTO PRODUCT_T (PRODUCT_ID,
PRODUCT_DESCRIPTION,PRODUCT_FINISH,
STANDARD_PRICE, PRODUCT_ON_HAND) VALUES (1, End
Table, Cherry, 175, 8);
Inserting from another table
Delete Statement
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE STATE =
HI;
Delete all rows
DELETE FROM CUSTOMER_T;
Update Statement
Modifies data in existing rows

UPDATE PRODUCT_T SET UNIT_PRICE =


775 WHERE PRODUCT_ID = 7;
Multiple Tables:
SQL provides a convenient
operation to retrieve information
from multiple tables.
This operation is called join.

The join operation will combine the tables into


one large table with all possible combinations
(Math: Cartesian Product), and then it will filter
the rows of this combined table to yield useful
information.
Multiple Tables:

field1 field2
A 1
field1 field2
A 2
A 1
A 3
B 2
B 1
3
B 2
B 3
Multiple Tables: Natural
Join
A Natural Join is a join operation that joins two
tables by their common column. This operation
is similar to the setting relation of two tables.

SELECT a.column 1, a.column2,


b.column2, expr1, expr2 ;
FROM table a, table b
WHERE a.column1 = b.column1 ;
Multiple Tables: Natural
Join
Make a list of students and the instruments they learn.
(Natural Join)
id name class id type

Same id 9801
9801

Join
Student Music

id name class type

9801
Product
Inner Join

A join that compares the table in the


FROM clause and lists only those
rows that satisfy the condition in the
WHERE clause in called an inner
join.

SELECT customer.customer_num,
Outer Join

The SQL OUTER JOIN clause is a


variation of the SQL JOIN clause
enables a SELECT statement to
access more than one table. The
JOIN clause controls how tables are
linked. It is a qualifier of the SQL
FROM clause.

SELECT customer.customer_num,

Вам также может понравиться