Presentation on theme: "Yong Choi School of Business CSU, Bakersfield"— Presentation transcript:
1 Yong Choi School of Business CSU, Bakersfield SQL AssignmentYong ChoiSchool of BusinessCSU, 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 informationLearn 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 SQLStandard Query Language (SQL) is the relational model’s 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 (con’t) SQL is relatively easy to learnSQL 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 SQLThree basic data functions by SQL, and their basic SQL commands:Data definition through the use of CREATEData manipulation through INSERT, UPDATE, and DELETEData 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 <attribute list>FROM <table list>WHERE <condition>GROUP BY < grouping attribute(s)>HAVING <group condition>ORDER BY <attribute list>
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_nameTo select all columns from a table, use a * symbol instead of column names: SELECT * FROM table_name
9 The WHERE StatementTo conditionally select data from a table, a WHERE clause can be added to the SELECT statement.Syntax:SELECT columnFROM tableWHERE column operator value
10 Typical Data Types INTEGER: Numbers without a decimal point SMALLINT: Uses less space than INTEGERDECIMAL(p,q): P number of digits; q number of decimal placesCHAR(n): Character string n places longDATE: Dates in DD-MON-YYYY or MM/DD/YYYY
11 SQL AssignmentAssignment 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 3List the complete Part table.
15 SQL – WHERE clauseAssignment Question 4: Save as Assignment Question 4List the name of every customers with $10,000 credit limit.Assignment Question 5: Save as Assignment Question 5Find the name of customer 148.
16 SQL Query with Where Condition Assignment Question 4SQL Query with Where Condition
17 SQL Query to Find Customer 148 Assignment Question 5SQL Query to Find Customer 148
18 SQL Comparison Operators FOR WHERE clause NOT Warehouse =‘3’LIKE: LIKE ‘a*’, LIKE ‘*s’, Like ‘*Oxford*’(NOT) BETWEEN AND 78000(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 GroveAssignment Question 7: Save as Assignment Question 7List the number, name, credit limit, and balance for customers with credit limits that exceed their balances.
22 SQL – Compound Conditions Assignment Question 8: Save as Assignment Question 8List 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 9List the descriptions of all parts that are located in warehouse 3 or for which there are more than 20 units on hand.
23 SQL Query with Compound Condition using ‘AND’ Assignment Question 8SQL Query with Compound Condition using ‘AND’
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 11List 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.
27 Query with ‘BETWEEN’ Operator Assignment Question 11Query with ‘BETWEEN’ Operator
28 SQL – Computed FieldComputed field can involve:addition(+), subtraction(-), Multiplication(*), or division (/)Assignment Question 12: Save as Assignment Question 12List the number, name and available credit for all customers.Assignment Question 13: Save as Assignment Question 13List the number, name, and available credit for all customers with credit limits that exceed their balances.
29 SQL Query with Computed Field Assignment Question 12SQL Query with Computed Field
30 SQL Query with Computed Assignment Question 13SQL Query with ComputedField and Condition
31 SQL – LIKE and INAssignment Question 14: Save as Assignment Question 14List the number, name, and complete address of every customer located on a street that contain the letters “Oxford.”Customer names begin with BCustomer names end with EAssignment Question 15: Save as Assignment Question 15List the number, name, and credit limit for every customer with a credit of $7,500, $10,000, or $15,000.
32 SQL Query with ‘LIKE’ Operator Assignment Question 14SQL Query with ‘LIKE’ Operator
33 SQL Query with ‘IN’ Operator Assignment Question 15SQL Query with ‘IN’ Operator
34 SQL Default value of ORDER BY: ascending Assignment Question 16: Save as Assignment Question 16List the number, name, and credit limit of all customers. Sort the customers by name in ascending order .Assignment Question 17: Save as Assignment Question 17List 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)