![Page 1: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/1.jpg)
Query Lab
CSC 240 Blum 1
![Page 2: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/2.jpg)
Log on to PMA (PHPMyAdmin) and click on the Northwind database
CSC 240 Blum 2
![Page 3: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/3.jpg)
Alternative place to work: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
CSC 240 Blum 3
![Page 4: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/4.jpg)
After choosing the Northwind database, click on the Query tab
CSC 240 Blum 4
![Page 5: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/5.jpg)
Use the drop-down to choose `customers`.* Then check the Show checkbox. Then click The Update Query button
CSC 240 Blum 5
The marks around the table name seen here are called tick marks or back-ticks. They slant and are above the Tab on the upper left of the keyboard. They are NOT single quotes. They are not necessary in this case but PMA includes them.
![Page 6: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/6.jpg)
Key words
• SELECT– In SQL a SELECT statement is one that looks at a
subset of data– As opposed to adding data (INSERT), taking away
data (DELETE) and changing data (UPDATE)
• FROM– FROM indicates the source of the data – which
tables that data comes from
CSC 240 Blum 6
![Page 7: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/7.jpg)
Next click the Submit Query button
CSC 240 Blum 7
![Page 8: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/8.jpg)
Result: 94 records (displaying 30 per page)
CSC 240 Blum 8
![Page 9: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/9.jpg)
Click on Northwind to move from the specific customers table to the more general Northwind database
CSC 240 Blum 9
![Page 10: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/10.jpg)
Click on the Query tab. This time select specific fields (instead of * for all fields). Remember to check Show and click Update Query.
CSC 240 Blum 10
![Page 11: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/11.jpg)
Result from clicking the Submit Query button. It produces the same number of records but fewer fields per record.
CSC 240 Blum 11
![Page 12: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/12.jpg)
Projection
• In “relational algebra” the previous query is an example of a projection – we have projected out some of the fields.
• (Don’t be intimidated by the term “relational algebra” it just means things we can do to database tables.)
CSC 240 Blum 12
![Page 13: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/13.jpg)
YOU DO #1
• Make a Word document and paste into the SQL for – The first name and last name of Employees
CSC 240 Blum 13
![Page 14: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/14.jpg)
Return to Northwind/Query. Choose `customers`.* and Show for one column and `customers`.`City` and ='London' as the Criteria
CSC 240 Blum 14
![Page 15: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/15.jpg)
Click Update to show the query
CSC 240 Blum 15
![Page 16: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/16.jpg)
WHERE – selection
• The new keyword appearing is WHERE – It is used to limit the record that will display to
those that meet some condition
• In relational algebra, this procedure of limiting the number of records is known as selection
• Note in this case that the WHERE condition uses single quotes – different from ticks
CSC 240 Blum 16
![Page 17: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/17.jpg)
Result of selecting customers whose city is London. 4 records.
CSC 240 Blum 17
![Page 18: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/18.jpg)
Compound condition: City is London or Berlin
CSC 240 Blum 18
![Page 19: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/19.jpg)
Results of Compound condition: City is London or Berlin
CSC 240 Blum 19
![Page 20: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/20.jpg)
YOU DO #2
• Paste into your Word document the SQL for – Suppliers whose Country is France or Germany
CSC 240 Blum 20
![Page 21: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/21.jpg)
Compound condition: contactTitle is Owner and Country is Mexico
CSC 240 Blum 21
![Page 22: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/22.jpg)
Result of Compound condition: contactTitle is Owner and Country is Mexico
CSC 240 Blum 22
![Page 23: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/23.jpg)
YOU DO #3
• Paste into your Word document the SQL for– Employees whose Title is sales Representative and
Country is USA
CSC 240 Blum 23
![Page 24: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/24.jpg)
Combining selection and projection
CSC 240 Blum 24
selectionprojection
![Page 25: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/25.jpg)
Result of selection and projection
CSC 240 Blum 25
![Page 26: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/26.jpg)
Sort result by choosing Ascending or descending from the drop-down under a field.
CSC 240 Blum 26
![Page 27: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/27.jpg)
Keywords for sorting
• ORDER BY– Is followed by the field (or fields) upon which the
sorting will be based• ASC– Indicates the results will be shown from lowest to
highest numerically or in alphabetic order for text/strings
• DESC– Highest to lowest or reverse alphabetical
CSC 240 Blum 27
![Page 28: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/28.jpg)
Results of query sorted on CompanyName
CSC 240 Blum 28
![Page 29: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/29.jpg)
YOU DO #4
• Paste into your Word document the SQL for – Products sorted by ProductName
CSC 240 Blum 29
![Page 30: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/30.jpg)
Obtaining a list of customer countries
CSC 240 Blum 30
![Page 31: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/31.jpg)
Result of customer country query (so far). Click on Edit to work on further.
CSC 240 Blum 31
![Page 32: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/32.jpg)
SQL editing dialog box arises
CSC 240 Blum 32
![Page 33: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/33.jpg)
Add keyword DISTINCT to eliminate duplicate country results
CSC 240 Blum 33
![Page 34: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/34.jpg)
Click Go. Query updated. And only unique countries occur in result
CSC 240 Blum 34
![Page 35: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/35.jpg)
YOU DO #5
• Paste into your Word document the SQL for– Distinct cities of employees
CSC 240 Blum 35
![Page 36: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/36.jpg)
Criteria other than equality: Products with a UnitPrice less than 20
CSC 240 Blum 36
![Page 37: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/37.jpg)
Result of Products with a UnitPrice less than 20
CSC 240 Blum 37
![Page 38: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/38.jpg)
Products with a UnitPrice greater than or equal to 30
CSC 240 Blum 38
![Page 39: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/39.jpg)
Result of Products with a UnitPrice greater than or equal to 30
CSC 240 Blum 39
![Page 40: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/40.jpg)
Switching >= to >
CSC 240 Blum 40
![Page 41: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/41.jpg)
A BETWEEN query criteria: UnitPrice between 20 and 30
CSC 240 Blum 41
![Page 42: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/42.jpg)
Results of BETWEEN query: UnitPrice between 20 and 30
CSC 240 Blum 42
![Page 43: Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2](https://reader036.vdocument.in/reader036/viewer/2022062410/5697c0181a28abf838cce32d/html5/thumbnails/43.jpg)
YOU DO #6
• Paste into your Word document the SQL for– Products having a UnitsInStock less than or equal
to 15
CSC 240 Blum 43