1 / 46

SQL – Part I

SQL – Part I. Yong Choi School of Business CSU, Bakersfield. 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

hawest
Download Presentation

SQL – Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SQL – Part I 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 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 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: • Data definition through the use of CREATE • Data manipulation through INSERT, UPDATE, and DELETE • 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 <attribute list> : Projection FROM <table list> : (Cartesian) Product [WHERE <condition>] : Restriction, Join [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_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. Semicolon after SQL Statements? • Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server. • MS Access and SQL Server 2000 do not require to put a semicolon after each SQL statement, but other database SQLs force you to use it such as Oracle. So, you must use a semicolon for this class. • Access SQL commands are not case sensitive (including table and attribute names) but try to follow exact names for better readability. • Download SQL data file form the class web site.

  12. CustomerNum OrderNum OrderNum RepNum PartNum LastName Description OrderDate PartNum CustomerName FirstName NumOrdered OnHand CustomerNum Class Street QuotedPrice Street Warehouse City Price State City Zip State Commission Zip Balance Rate CreditLimit RepNum 148 21608 20 21608 AT94 Kaiser Iron 10/20/2003 AT94 Al's Appliance and Sport Valerie 11 50 148 HW 624 Randall $21.95 2837 Greenway 3 Grove $24.95 FL Fillmore 33321 FL $20,542.50 33336 $6,550 0.05 $7,500 20 Customer OrderLine Order Part Rep

  13. SQL Examples • Example 2: Save as example 2 • List the number, name, and balance of all customers.

  14. Simple Retrieval

  15. SQL Examples • Example 3: Save as example 3 • List the complete Part table.

  16. SQL Query to List Part Table

  17. Oracle 7.0 SQL Example SQL> select * from employee; EMP_ID EMP_LNAME EMP_FNAME EMP_SALARY EMP_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.

  18. SQL Examples – WHERE clause • Example 4: Save as example 4 • List the name of every customers with $10,000 credit limit.

  19. SQL Query with Where Condition

  20. SQL Examples – WHERE clause • Example 5: Save as example 5 • Find the name of customer 148.

  21. SQL Query to Find Customer 148

  22. SQL Comparison Operators FOR WHERE clause NOT Warehouse =‘3’ LIKE: LIKE ‘a*’, LIKE ‘*s’, Like ‘*Oxford*’ (NOT) BETWEEN 45000 AND 78000 (NOT) IN (123, 345)

  23. SQL Examples • Example 6: Save as example 6 • Find the customer name for every customer located in the city of Grove

  24. SQL Query to Find All Customers in ‘Grove’

  25. SQL Examples • Example 7: Save as example 7 • List the number, name, credit limit, and balance for customers with credit limits that exceed their balances. • How about by OBE (Access way)?

  26. Query to find Customers with Credit Limit Exceeding Balance

  27. SQL Examples – Compound Conditions • Example 8: Save as example 8 • List the description of all parts that are located in warehouse 3 andfor which there are more than 20 units on hand.

  28. SQL Query with Compound Condition using ‘AND’

  29. SQL Examples – Compound Conditions • Example 9: Save as example 9 • List the descriptions of all parts that are located in warehouse 3 orfor which there are more than 20 units on hand.

  30. SQL Query using ‘OR’

  31. SQL Examples • Example 10: Save as example 10 • List the description of all parts that are not in warehouse 3. • Using NOT: “<>” • How about by QBE?

  32. SQL Query using ‘NOT’

  33. SQL Examples • Example 11: Save as example 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.

  34. Query with ‘BETWEEN’ Operator

  35. SQL Examples – Computed Field • Computed field can involve: • addition(+), subtraction(-), Multiplication(*), or division (/) • Example 12: Save as example 12 • List the number, name and available credit for all customers. • How about by QBE?

  36. SQL Query with Computed Field

  37. SQL Examples – Computed Field • Computed field can involve: • addition(+), subtraction(-), Multiplication(*), or division (/) • Example 13: Save as example 13 • List the number, name, and available credit for all customers with credit limits that exceed their balances. • How about by QBE?

  38. SQL Query with Computed Field and Condition

  39. SQL Examples – LIKE and IN • Example14: Save as example 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

  40. SQL Query with ‘LIKE’ Operator

  41. SQL Examples – LIKE and IN • Example15: Save as example 15 • List the number, name, and credit limit for every customer with a credit of $7,500, $10,000, or $15,000. • Using “IN” command • How about by QBE?

  42. SQL Query with ‘IN’ Operator

  43. SQL Examples • Default of ORDER BY: ascending • Example16: Save as example 16 • List the number, name, and credit limit of all customers. Sort the customers by name in ascending order. • How about by QBE?

  44. SQL Query to Sort Data

  45. SQL Examples • Example17: Save as example 17 • List the number, name, and credit limit of all customers. Sort the customers by name in ascending order within credit limit in descending order. • How about by QBE?

  46. SQL Query to Sort on Multiple Fields

More Related