Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 4 - Simple Queries & More on Tables

Similar presentations


Presentation on theme: "Day 4 - Simple Queries & More on Tables"— Presentation transcript:

1 Day 4 - Simple Queries & More on Tables
What is a query in MySQL? Basic Format of a Query More Actions on Tables Via Queries

2 Day 4 - Simple Queries & More on Tables
What is a query in MySQL? SQL = Structured Query Language MySQL is based on the ideas of Relational Algebra and Boyce Codd Normal Form (BCNF). A query is basically a formatted question to get meaningful information out of a DB. CSE 498 Day 4 - Simple Queries & More on Tables

3 Day 4 - Simple Queries & More on Tables
Basic Format of a Query Basic queries have three parts... SELECT <column_name> FROM <table_name(s)> WHERE <argument> SELECT allows you to get particular columns of information in the output of the query. SELECT * will select all the columns available. FROM selects the table(s) that the query is going to use to produce the resulting information. WHERE is a TRUE/FALSE resulting clause. Only sets of information that appear as true will be included in the result table. A WHERE clause is not mandatory. CSE 498 Day 4 - Simple Queries & More on Tables

4 Example DB Schema: Movie DB
For this lecture, we are going to use the movie DB table ‘Customer’ for the example. The table for customer has the following format. Customer (CustID, LastName, FirstName, Age, Address) CSE 498 Day 4 - Simple Queries & More on Tables

5 Querying by an Attribute
Allows us to cut down the amount of table rows in a result query. Accomplished by writing an argument in the WHERE clause. Example: What people have the last name ‘Lee’? SELECT * FROM Customer C WHERE C.LastName = “Lee” CSE 498 Day 4 - Simple Queries & More on Tables

6 Day 4 - Simple Queries & More on Tables
Querying by a Column Allows us to cut down the amount of table columns in a result query. Accomplished by picking columns in the select clause. Example: Give me the first names of the people who share the last name ‘Lee’? SELECT C.FirstName FROM Customer C WHERE C.LastName = “Lee” CSE 498 Day 4 - Simple Queries & More on Tables

7 Duplicate Information Problem
Cross referencing table information or just cutting down the number of columns you view can produce duplicate result information. Duplicates can be removed by using the word DISTINCT in the SELECT clause. This will make sure that there is only one instance of a given column in the result. Example: What are all the last names of the customers? SELECT DISTINCT C.LastName FROM Customer C CSE 498 Day 4 - Simple Queries & More on Tables

8 Day 4 - Simple Queries & More on Tables
Sorting Information Data is often added to a DB in random order. Thus, getting an ordered list can be a pain if you have to do it on your own. SQL gives you the command ORDER BY that follows the WHERE clause. This command is defaulted to order things in descending order. Example: Give me an ordered list of the customers’ last names. SELECT DISTINCT C.LastName FROM Customer C ORDER BY C.LastName CSE 498 Day 4 - Simple Queries & More on Tables

9 Day 4 - Simple Queries & More on Tables
Grouping Information Sometimes, it is necessary to group similar items together. The GROUP BY command groups items together into bunches. This command follows the WHERE clause... This is often used with a HAVING clause. CSE 498 Day 4 - Simple Queries & More on Tables

10 Day 4 - Simple Queries & More on Tables
The HAVING clause The HAVING clause is often used with aggregate functions. Aggregate functions are explained on Day 5 along with JOIN. The HAVING clause is a way to isolate groups of information. Refer back to this example later... Example: What ages of customers have more than 2 people in their bracket? SELECT C.Age FROM Customer C GROUP BY C.Age HAVING COUNT(C.Age) > 2; CSE 498 Day 4 - Simple Queries & More on Tables

11 Day 4 - Simple Queries & More on Tables
AS command The AS command allows to to rename relational sets or column names as necessary… Example: Give me an ordered list of the customers’ last names. SELECT DISTINCT C.LastName AS Cust_Last_Name FROM Customer C ORDER BY C.LastName This query would return the result set... Cust_Last_Name Alway Ammerman Becker CSE 498 Day 4 - Simple Queries & More on Tables


Download ppt "Day 4 - Simple Queries & More on Tables"

Similar presentations


Ads by Google