Presentation is loading. Please wait.

Presentation is loading. Please wait.

Projecting output in MySql

Similar presentations


Presentation on theme: "Projecting output in MySql"— Presentation transcript:

1 Projecting output in MySql
Projection is used to eliminate some of the components of the chosen tuple(s). We do the projection by listing any of the attributes of the relation mentioned in the FROM clause in the SELECT clause. The result of the query will be projected onto the attribute(s) listed. CSE 498 MySql In Action

2 Projection example - Query
Suppose that you only want to know the address of your customers whose last name is Smith. You can use the following query to retrieve it. select address from Customers where lastName = 'Smith'; Note: List the name of the attribute that you want in the SELECT clause. In the FROM clause, list the name of the table In the WHERE clause, provide the condition. CSE 498 MySql In Action

3 Projection example - Result
The result of the query might look like the following: CSE 498 MySql In Action

4 Selecting records in MySql
You actually have already seen some selections from the previous slides. The selection in MySql is available through the WHERE clause. The expressions that may follow WHERE clause include conditional expressions. >, <, =, =>, <=, and <> CSE 498 MySql In Action

5 Selecting records in MySql (Cont.)
The values that may be compared include constants and attributes of the relations mentioned after FROM clause. We may also apply usual arithmetic operators, such as +, -, *, and so on, to numeric values before the comparison. String concatenation operator ( || ) is also legal. CSE 498 MySql In Action

6 Selection example - Query
Suppose that you want to know what movie titles that have more than or equal to four copies. From the previous description about selection, you might come up with the following query. select title from Rentals where copyNum >= 4; CSE 498 MySql In Action

7 Selection example - Result
You should get the similar result as the screenshot below. CSE 498 MySql In Action

8 String comparison in MySql
Like any other programming languages, MySql also has a string comparison operator. We can use one of the “less than” operators, such as >, <, <=, or >=. We can also use LIKE operator if we know the pattern of the string that we want to compare with. CSE 498 MySql In Action

9 Like operator S like P means that a string S has a pattern P.
There are two operators that can be used in pattern: % and _. % = any sequences of zero or more characters in string S. _ = any one character in string S. Example: book LIKE ‘b__k’ CSE 498 MySql In Action

10 String comparison example - Query
If you want to retrieve information about a movie whose title starts from S, you should come up with the following query: Select * From Rentals Where title like ‘S%’; Notice that we use the string comparison at the last line. CSE 498 MySql In Action

11 String comparison example - Result
The result of the latest query is as the following: CSE 498 MySql In Action

12 Sorting the output To sort the output of a query, we can use ORDER BY operator The syntax for ORDER BY operator: ORDER BY <list of attributes> Example: if you want to sort the customer names based on their first name, you should use select * from Customers order by firstName; CSE 498 MySql In Action

13 Sorting result The result of the query will be similar to CSE 498
MySql In Action

14 Joining table in MySql To do a join in MySql, you should do the following steps: 1. List each relation in the FROM clause 2.The SELECT and WHERE clauses can refer to the attributes of any of the relations in the FROM clause CSE 498 MySql In Action

15 Join example - Query If you want to list all the name of the customers who borrow movie from your Rental and the amount of money they have to pay. Select firstName, lastName, cost From Customers, CheckOut Where Customers.customerID = CheckOut.customerID; CSE 498 MySql In Action

16 Join example - Result After executing the query, you should get the same result as the following Since the Customers table and CheckOut table have the same attribute - CustomerID, we have to mention the name of the table when we use it. CSE 498 MySql In Action

17 Using aggregation in MySql
There are some aggregation functions in MySql Sum = adding all values in an attribute Min = minimum value in an attribute Max = maximum value in an attribute Avg = average value in an attribute Count = count the number of record in an attribute CSE 498 MySql In Action

18 Using aggregation function in the query
Suppose that you want to know how much money that you can collect from your customer. One way to get it is to use Sum function in your query. CSE 498 MySql In Action

19 Using aggregation function (Cont.)
You also can know how many videos your customers rent so far by using Count function. (See the following screenshot) CSE 498 MySql In Action

20 Grouping outputs You can also group the output of the query according to certain category by using GROUP BY clause. The default setting of GROUP BY clause is in ascending order. GROUP BY clause is usually located after the SELECT-FROM-WHERE clause CSE 498 MySql In Action

21 Grouping outputs - Example
To group the customer names in the Customers table, you might use this query: Select * From Customers Group by firstName; CSE 498 MySql In Action

22 Having clause Usually HAVING clause is used together with GROUP BY clause It is used to select certain records in each group of the output. The syntax of HAVING clause Having <condition> Usually, HAVING clause is found after GROUP BY clause CSE 498 MySql In Action

23 Having clause - Example
Suppose that you want to display the customer IDs and the amount of money they have to pay for renting the film based upon the movieID and cost is grater than $2.00 CSE 498 MySql In Action

24 Order is important The order in which each clause should be put in the query is 1. SELECT clause 2. FROM clause 3. WHERE clause 4. GROUP BY clause 5. HAVING clause 6. ORDER BY clause CSE 498 MySql In Action


Download ppt "Projecting output in MySql"

Similar presentations


Ads by Google