1 / 13

CS122 Using Relational Databases and SQL Single-Table Selections

CS122 Using Relational Databases and SQL Single-Table Selections. Chengyu Sun California State University, Los Angeles. Selection Queries. Retrieve the records that satisfy certain conditions. select field(s) from table(s) [ where condition(s)]

jerrod
Télécharger la présentation

CS122 Using Relational Databases and SQL Single-Table Selections

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. CS122 Using Relational Databases and SQLSingle-Table Selections Chengyu Sun California State University, Los Angeles

  2. Selection Queries • Retrieve the records that satisfy certain conditions select field(s) from table(s) [where condition(s)] [order by field(s) asc|desc]; SQL is case insensitive.

  3. Selection Query Examples • List the descriptions of all products • List all products • List all CPU products • List all products that cost more than $80, and order the results by price in descending order • List the orders placed in this month • List all the Intel products • List the orders that are not shipped yet

  4. Values • Numbers: 10, 30.2 • Strings: ‘CPU’, ‘John’’s Kitchen’ • Dates: ‘2007-06-01’ • NULL

  5. Arithmetic +, -, *, /, % Comparison <, >, <=, >=,=,<> between Logical and, or, not String like Other is null in Operators

  6. LIKE • Pattern matching • %: any zero or more characters • .: any single character • [abc], [a-z], [0-9]: range • * -- zero or more instances of the preceding character select * from products where description like ‘%intel%’;

  7. About NULL • NULL is a special value representing “empty”, “no value”, or “unknown” • Whether an attribute is NULL or not can be checked with IS NULL or IS NOT NULL, but not with comparison operators like = or <>

  8. Expressions (1-0.1) * price category = ‘CPU’ and price > 100 description like ‘%intel%’ order_date between ‘2007-5-19’ and ‘2007-6-17’ and shipping_date is null

  9. More Selection Query Examples • Find the prices of the CPU products after 10% discount • Find the hard drives made by Maxtor • Find the orders made last month but have not been shipped yet

  10. CASE: switch style SELECT product_id, CASE category WHEN ‘MB’ THEN ‘Motherboard’ WHEN ‘CPU’ THEN ‘Processor’ ELSE ‘ERROR!’ END FROM products;

  11. CASE: if-else style SELECT product_id, CASE WHEN Price > 200 THEN ‘Expensive’ ELSE ‘Cheap’ END FROM products;

  12. DISTINCT • Remove duplicate results select distinct address from customers;

  13. Column Aliases • Name the result columns select description as‘Intel CPUs’ from products where category = ‘CPU’ and description like ‘%Intel%’;

More Related