Presentation on theme: "SQL Assignment Yong Choi School of Business CSU, Bakersfield."— Presentation transcript:
SQL Assignment Yong Choi School of Business CSU, Bakersfield
2 Study Objectives Understand the basic commands and functions of SQL Learn how SQL is used for data manipulation (to add, modify, delete, and retrieve data) Learn how to use SQL to query a database to extract useful information Learn how SQL is used for data administration (to create tables, indexes, and views) Practice SQL
3 Ideal Database Language Requirements Create database and table structures. –SQL has a data definition component that gives us the ability to meet this requirement. Manage the data component of the database. –SQL gives us a set of commands to add, update, and delete data within the database tables. Provide detailed data query capability. –"Standard" SQL uses a set of approximately thirty commands that allow us to retrieve data and to convert the raw data into useful information.
4 Introduction to SQL Standard Query Language (SQL) is the relational models standard language. The original version of SQL was developed at IBM's San Jose Research Laboratory. This language, originally called Sequel. The Sequel language has evolved since then, and its name has changed to SQL (Structured Query Language). In 1986, the American National Standards Institute (ANSI) published an SQL standard. –In 1992, work was completed on a significantly revised version of the SQL standard (SQL-92).
5 Introduction to SQL (cont) SQL is relatively easy to learn –SQL commands set has a basic vocabulary of less than 100 words. SQL is a nonprocedural language. So, it is much easier to use. –Its user merely commands what is to be done without having to worry about how it's to be done. –Procedural language: COBOL, C, or Pascal.
6 More about SQL Three basic data functions by SQL, and their basic SQL commands: 1.Data definition through the use of CREATE 2.Data manipulation through INSERT, UPDATE, and DELETE 3.Data querying through the use of SELECT AND MANY OTHERS, which is the basis for all SQL queries. –We will try this first since we just completed all Access queries.
7 Basic Structure of SQL Queries SQL relation is not a set of tuples because a set does not allow two identical members; rather it is a multi-set (a bag) of tuples. A SQL query has the form: SELECT FROM WHERE GROUP BY HAVING ORDER BY
8 The SELECT and FROM Statement The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result set). The FROM statement is used to select tables. Syntax: –SELECT column_name(s) –FROM table_name To select all columns from a table, use a * symbol instead of column names: –SELECT * FROM table_name
9 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
10 Typical Data Types INTEGER: Numbers without a decimal point SMALLINT: Uses less space than INTEGER DECIMAL(p,q): P number of digits; q number of decimal places CHAR(n): Character string n places long DATE: Dates in DD-MON-YYYY or MM/DD/YYYY
11 SQL Assignment Assignment questions start from the next slide. Answer Table is given as a hint for each question.
12 SQL Assignment Question 2: Save as Assignment Question 2 –List the number, name, and balance of all customers. Assignment Question 3: Save as Assignment Question 3 –List the complete Part table.
13 Assignment Question 2
14 Assignment Question 3
15 SQL – WHERE clause Assignment Question 4: Save as Assignment Question 4 –List the name of every customers with $10,000 credit limit. Assignment Question 5: Save as Assignment Question 5 –Find the name of customer 148.
16 Assignment Question 4 SQL Query with Where Condition
18 SQL Comparison Operators FOR WHERE clause NOT Warehouse =3 LIKE: LIKE a*, LIKE *s, Like *Oxford* (NOT) BETWEEN AND (NOT) IN (123, 345)
19 SQL Assignment Question 6: Save as Assignment Question 6 –Find the customer name for every customer located in the city of Grove Assignment Question 7: Save as Assignment Question 7 –List the number, name, credit limit, and balance for customers with credit limits that exceed their balances.
20 Assignment Question 6
21 Assignment Question 7
22 SQL – Compound Conditions Assignment Question 8: Save as Assignment Question 8 –List the description of all parts that are located in warehouse 3 and for which there are more than 20 units on hand. Assignment Question 9: Save as Assignment Question 9 –List the descriptions of all parts that are located in warehouse 3 or for which there are more than 20 units on hand.
23 Assignment Question 8 SQL Query with Compound Condition using AND
24 Assignment Question 9 SQL Query using OR
25 SQL Assignment Question 10: Save as Assignment Question 10 –List the description of all parts that are not in warehouse 3. Assignment Question 11: Save as Assignment Question 11 –List the number, name, and balance of all customers with balances greater than or equal to $1,000 and less than or equal to $5,000.
26 Assignment Question 10 SQL Query using NOT
27 Assignment Question 11 Query with BETWEEN Operator
28 SQL – Computed Field Computed field can involve: –addition(+), subtraction(-), Multiplication(*), or division (/) Assignment Question 12: Save as Assignment Question 12 –List the number, name and available credit for all customers. Assignment Question 13: Save as Assignment Question 13 –List the number, name, and available credit for all customers with credit limits that exceed their balances.
29 Assignment Question 12 SQL Query with Computed Field
30 Assignment Question 13 SQL Query with Computed Field and Condition
31 SQL – LIKE and IN Assignment Question 14: Save as Assignment Question 14 –List the number, name, and complete address of every customer located on a street that contain the letters Oxford. Customer names begin with B Customer names end with E Assignment Question 15: Save as Assignment Question 15 –List the number, name, and credit limit for every customer with a credit of $7,500, $10,000, or $15,000.
32 Assignment Question 14 SQL Query with LIKE Operator
33 Assignment Question 15 SQL Query with IN Operator
34 SQL Default value of ORDER BY: ascending Assignment Question 16: Save as Assignment Question 16 –List the number, name, and credit limit of all customers. Sort the customers by name in ascending order. Assignment Question 17: Save as Assignment Question 17 –List the number, name, and credit limit of all customers. Sort the customers by name (minor) in ascending order within credit limit (major) in descending order. –What about this case? customer name (minor) and credit limit (major)
35 Assignment Question 16 SQL Query to Sort Data
36 Assignment Question 17 SQL Query to Sort on Multiple Fields