Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.
-
date post
22-Dec-2015 -
Category
Documents
-
view
217 -
download
0
Transcript of SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.
![Page 1: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/1.jpg)
SQL (Standard Query Language)
Yong Choi
School of Business
CSU, Bakersfield
![Page 2: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/2.jpg)
2
Study Objectives
• Understand the basic commands and functions of SQL
• Learn how to use SQL to query a database to extract useful information
• Practice SQL
![Page 3: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/3.jpg)
3
Introduction to SQL
• Standard Query Language (SQL) is a computer language for the relational database model.
• SQL is a nonprocedural language (click here for the example). So, it is much easier to use. – what is to be done without having to worry about how
it's to be done.• Procedural language (click here for the example)
– Must be programmed correctly and compiled– Java, C++, and Pascal.
• SQL is relatively easy to learn– SQL commands set has a basic vocabulary of less than
100 words.
Go To Next Topic
![Page 4: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/4.jpg)
4
SQL Example
SELECT ID, L_Name, F_Name, Salary, Dept_No
FROM Employee;
ID L_Name F_Name Salary Dept_No
--------- ---------- ---------- ---------- -------------------------------------------------------------------------
1 Kim John 1000 100
2 Johnson Steve 1200 100
3 Jonson Paul 1100 200
4 Lee Jim 1100 200
5 Basinger Jon 1300
6 Stone Sharon 1000
6 rows selected.
Back
![Page 5: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/5.jpg)
5
JAVA Example
public class JavaProgramming{
public static void main ( String[] args ) { long payAmount; payAmount = 123; System.out.println("The variable contains: " + payAmount ); }
}
• Output: The Variable contains 123.
Back
![Page 6: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/6.jpg)
6
Basic SQL Commands
• Followings are the most frequently used commands – Use always
• SELECT <field list>• FROM <table list>
– Use when conditions must be specified • WHERE <condition>• HAVING <group condition>
• ORDER BY <sorting field>• GROUP BY < grouping records>
![Page 7: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/7.jpg)
7
The SELECT and FROM Statement
• Need both commands almost always…..– The SELECT statement is used to select data from
a table. – The FROM statement is used to select tables.
• Syntax:– SELECT column_name(s) – FROM table_name
• To select all columns (fields) from a table, use * symbol instead of column names: – SELECT * – FROM table_name
![Page 8: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/8.jpg)
8
The WHERE Statement
• To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.
• Syntax:– SELECT column
– FROM table
– WHERE column operator value • See next slide for various operators
![Page 9: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/9.jpg)
9
SQL Comparison Operators FOR WHERE clause
AND logical operator
OR logical operator
NOT Warehouse =‘3’
LIKE: LIKE ‘a*’, LIKE ‘*s’, Like ‘*Oxford*’
BETWEEN 45000 AND 78000
![Page 10: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/10.jpg)
10
Semicolon after SQL Statements?
• Semicolon is the standard way to a block of SQL statement in database systems. So, you must use a semicolon at the end of a block of SQL statement.– Access SQL commands are not case sensitive but
try to follow exact names for better readability.
• Download SQL data file form the class web site.– SQL_300.mdb
![Page 11: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/11.jpg)
11
CustomerNum CustomerName Street City State Zip Balance CreditLimit RepNum
148 Al's Appliance and Sport
2837 Greenway
Fillmore FL 33336 $6,550 $7,500 20
OrderNum PartNum NumOrdered QuotedPrice
21608 AT94 11 $21.95
OrderNum OrderDate CustomerNum
21608 10/20/2003 148
Customer
OrderLine
Orders
PartNum Description OnHand Class Warehouse Price
AT94 Iron 50 HW 3 $24.95
Part
Rep
RepNum LastName FirstName Street City State Zip Commission Rate
20 Kaiser Valerie 624 Randall Grove FL 33321 $20,542.50 0.05
![Page 12: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/12.jpg)
12
SQL Preparation
• Review tables and relationships first…– Review each table in both views
• Design view• Datasheet view
– Recognize PK and FK information
– Examine Relationships information
• SQL using Access– Design view of query Select SQL View using
Style button
– No need to add tables (unlike QBE)
![Page 13: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/13.jpg)
13
SQL Sample
• I’d like to know the list of CustomerNum and CustomerName.– SELECT CustomerNum, CustomerName– FROM Customer;
• I’d like to know the list of PartNum, Description, RepNum, and LastName– SELECT PartNum, Description, RepNum, LastName– FROM Part, Rep;
![Page 14: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/14.jpg)
14
SQL Example 1 & 2
• Example 1: – I’d like to know a list of the Customer number,
Customer name, and balance of all customers.– Save as SQL 1
• Example 2:– I’d like to know a list of the Order number, Part
number, Price and Order Date.– Save as SQL 2
![Page 15: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/15.jpg)
15
SQL Example 3
• Restriction requirements– Where command– Exact Match
• Example 3: – I’d like to know customer names who are located
in the city of Grove– Save as SQL 3
![Page 16: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/16.jpg)
16
SQL Query to Find All Customers in ‘Grove’
![Page 17: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/17.jpg)
17
SQL Example 4
• Restriction requirements– Where command– Comparison operator
• Example 4:– I’d like to know a list of the number, name, credit
limit, and balance for customers with credit limits that exceed their balances.
– Save as SQL 4
![Page 18: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/18.jpg)
18
Query to find Customers with Credit Limit Exceeding Balance
![Page 19: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/19.jpg)
19
SQL Example 5
• Restriction requirements– Where command– Compound conditions
• Example 5: – List the description of all parts that are located in
warehouse 3 and for which there are more than 20 units on hand.
– Save as SQL 5
![Page 20: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/20.jpg)
20
SQL Query with Compound Condition using ‘AND’
![Page 21: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/21.jpg)
21
SQL Example 6
• Default value of ORDER BY: ascending
• Example 6:– List the number, name, and credit limit of all
customers. Sort the customers by name in ascending order.
– Save as SQL 6
![Page 22: SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d7a5503460f94a5e668/html5/thumbnails/22.jpg)
22
SQL Query to Sort Data