Query Lab CSC 240 Blum1
Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2
Alternative place to work: CSC 240 Blum3
After choosing the Northwind database, click on the Query tab CSC 240 Blum4
Use the drop-down to choose `customers`.* Then check the Show checkbox. Then click The Update Query button CSC 240 Blum5 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.
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 Blum6
Next click the Submit Query button CSC 240 Blum7
Result: 94 records (displaying 30 per page) CSC 240 Blum8
Click on Northwind to move from the specific customers table to the more general Northwind database CSC 240 Blum9
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 Blum10
Result from clicking the Submit Query button. It produces the same number of records but fewer fields per record. CSC 240 Blum11
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 Blum12
YOU DO #1 Make a Word document and paste into the SQL for – The first name and last name of Employees CSC 240 Blum13
Return to Northwind/Query. Choose `customers`.* and Show for one column and `customers`.`City` and ='London' as the Criteria CSC 240 Blum14
Click Update to show the query CSC 240 Blum15
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 Blum16
Result of selecting customers whose city is London. 4 records. CSC 240 Blum17
Compound condition: City is London or Berlin CSC 240 Blum18
Results of Compound condition: City is London or Berlin CSC 240 Blum19
YOU DO #2 Paste into your Word document the SQL for – Suppliers whose Country is France or Germany CSC 240 Blum20
Compound condition: contactTitle is Owner and Country is Mexico CSC 240 Blum21
Result of Compound condition: contactTitle is Owner and Country is Mexico CSC 240 Blum22
YOU DO #3 Paste into your Word document the SQL for – Employees whose Title is sales Representative and Country is USA CSC 240 Blum23
Combining selection and projection CSC 240 Blum24 selection projection
Result of selection and projection CSC 240 Blum25
Sort result by choosing Ascending or descending from the drop-down under a field. CSC 240 Blum26
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 Blum27
Results of query sorted on CompanyName CSC 240 Blum28
YOU DO #4 Paste into your Word document the SQL for – Products sorted by ProductName CSC 240 Blum29
Obtaining a list of customer countries CSC 240 Blum30
Result of customer country query (so far). Click on Edit to work on further. CSC 240 Blum31
SQL editing dialog box arises CSC 240 Blum32
Add keyword DISTINCT to eliminate duplicate country results CSC 240 Blum33
Click Go. Query updated. And only unique countries occur in result CSC 240 Blum34
YOU DO #5 Paste into your Word document the SQL for – Distinct cities of employees CSC 240 Blum35
Criteria other than equality: Products with a UnitPrice less than 20 CSC 240 Blum36
Result of Products with a UnitPrice less than 20 CSC 240 Blum37
Products with a UnitPrice greater than or equal to 30 CSC 240 Blum38
Result of Products with a UnitPrice greater than or equal to 30 CSC 240 Blum39
Switching >= to > CSC 240 Blum40
A BETWEEN query criteria: UnitPrice between 20 and 30 CSC 240 Blum41
Results of BETWEEN query: UnitPrice between 20 and 30 CSC 240 Blum42
YOU DO #6 Paste into your Word document the SQL for – Products having a UnitsInStock less than or equal to 15 CSC 240 Blum43