Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Intermediate Workshop Authored by Jay Mussan-Levy.

Similar presentations


Presentation on theme: "SQL Intermediate Workshop Authored by Jay Mussan-Levy."— Presentation transcript:

1 SQL Intermediate Workshop Authored by Jay Mussan-Levy

2 Introduction to Querying The power of SQL lies not in the creation of tables, but the ability to attain information from them. Querying is asking the database if it has certain things, then the database gives you the results.

3 Select Statement The way to query in SQL is through the select statement The select statement has five main clauses but “from” is the only required ones Here is the syntax for a select statement: select all|distinct column1, column2 from table1, table2 where "some condition" group by "some condition" having "some condition" order by "some condition"

4 Select Statement cont’d Here is an example of a simple query: select quantity, max(price) from items_ordered group by quantity; This gets displays the quantity for the most costly item for every quantity number, and its corresponding price. Groups by quantity (staring with 1)

5 Select Statement cont’d Here is what the results would look like: 11250.00 288.70 314.75 4125.00

6 Select Statement cont’d Comparison Operators = equals > greater than < less than >= greater than or equal to <=less than or equal to <> or != not equal to LIKEstring comparison test

7 Select Statement cont’d Example: select first, last, title from employee where title LIKE 'Pro%'; Now the previous statement gets the first name, last name, and title columns for all employees whose title begins with Pro

8 Select Statement cont’d BeforeAfter

9 Select Statement All and Distinct ALL and DISTINCT will select either all or unique records ex) Select DISTINCT age from employee_info; This will return all the unique ages you have in the table

10 Select Statement All and Distinct cont’d BeforeAfter

11 Select Statement Review Please go to http://sqlcourse2.com/select2.htmlhttp://sqlcourse2.com/select2.html Scroll to the bottom of the page and do Problems 2 and 3

12 Select Statement Aggregate Functions Aggregate Functions are: Min - Returns the smallest value in a given column Max - Returns the largest value in a given column Sum - Returns the sum of the numeric values in a given column Avg - Returns the average value of a given column Count - Returns the total number of values in a given column Count(*) - returns the number of rows in a table

13 Select Statement Aggregate Functions cont’d Aggregate values are used to manipulate numeric values in a column. Here is an example: Select avg(salary) from employee;

14 Select Statement Aggregate Functions cont’d Before After 57,803.33

15 Select Statement Group By The GROUP BY clause will gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions to be performed on the one or more columns.

16 Select Statement Group By cont’d Here is the syntax: Select "columnname", SUM("columnname2") from "tablename" Group By “some column name";

17 Select Statement Group By cont’d Select max(salary), title from employee group by title; This statement will select the maximum salary for each unique position. Basically, the salary for the person who makes the most in each position will be displayed. Their, name, salary, and their position will be returned.

18 Select Statement Group By cont’d Before After

19 Select Statement Group By cont’d How do you group everything of quantity 1 together, quantity 2 together, quantity 3 together, etc. ? What if you wanted to determine the largest cost item for each grouped quantity? Here is how: Select quantity, max(price) from items_ordered group by quantity

20 Select Statement Group By cont’d Results

21 Select Statement Having Clause Must follow the Group By clause Allows you to specify conditions on the rows for each group Which rows should be selected will be based on the conditions you specify

22 Select Statement Having Clause cont’d The syntax for Having is: Select "columnname", sum("columnname2") from "tablename" group by "columnlist" having "condition";

23 Select Statement Having Clause cont’d Let's say you have an employee table containing the employee's name, title, salary, and age. If you would like to select the average salary for each position,you could enter: Select title, avg(salary) from employee group by title;

24 Select Statement Having Clause cont’d Before After

25 Select Statement Having Clause cont’d But what if you only wanted to display the average if their salary was over 20000? select quantity, avg(price) from items_ordered group by quantity having avg(price) > 20;

26 Select Statement Having Clause cont’d Results

27 Select Statement Order By Order by is sorting through a rule you create Ascending descending Syntax: Select "columnname", Sum("columnname2") from "tablename" order by "rule";

28 Select Statement Order By Example: Select first, last, id, age, state from employee_info where state = ‘Arizona’ order by last; This statement will select 5 columns, where the State is Arizona. It will sort them by last name in ascending order. If you wanted it to be descending, change the last line to: order by last desc;

29 Select Statement Order By cont’d Before After

30 Select Statements Combining Conditions cont’d The AND operator – Used to join 2 conditions in the WHERE clause. Both sides of the AND condition must be true for the condition to be met and for those rows to be displayed The OR operator – Used to join two conditions in the WHERE clause. Either side of the OR operator can be true and the condition will be met or both sides can be true

31 Select Statements Combining Conditions and Boolean Operations Syntax: Select "columnname", sum("columnname2") from "tablename" where "condition1" and "condition2";

32 Select Statements Combining Conditions and Boolean Operations example: Select first, last, id, age, city, state from employee_info where (age >= 30) and (state = ‘Arizona');

33 Select Statements Combining Conditions and Boolean Operations Before After

34 Select Statements Combining Conditions and Boolean Operations Example: Select first, last, id, age, city, state from employee_info where (age >= 30) or (state = ‘Arizona');

35 Select Statements Combining Conditions and Boolean Operations Before After

36 Select Statement In and Between Conditional Operators The In Operator can be thought of as an or Statement. This is why: where lastname IN('Hernandez', 'Jones', 'Roberts', 'Ruiz'); is the same as where lastname = (‘Hernandez’) or lastname = (‘Jones’) or lastname = (‘Roberts’) or lastname = ‘Ruiz’); You can also use not in to exclude people

37 Select Statement In and Between Conditional Operators cont’d The between operator is used to find something within a range of numbers Here is an example: Select employeeid, age, lastname, salary from employee_info where age Between 30 and 40; This is the same as where age >= 30 and age <= 40; You can also use not between

38 Select Statements Mathematical Functions The Mathematical Operators are: +addition - subtraction *multiplication / division %modulos (remainder)

39 Select Statements Mathematical Functions cont’d ABS(x)absolute value of x SIGN(x)-1, 0, 1, returns positive, negative, zero MOD(x,y)returns the remainder of x/y (same as x%y) FLOOR(x)returns the largest integer less than or equal to x CEILING(x) or CIEL(x)returns the smallest Integer that is >= x

40 Select Statements Mathematical Functions cont’d POWER(x,y)raise x to the y power ROUND(x)returns x rounded to the nearest integer ROUND(x,d)returns the value of x rounded to the number of decimal places specified by d SQRT(x)returns the square-root value of x

41 Select Statements Mathematical Functions cont’d Here is an example: select round(salary), first, last from employee; This means that everyone’s salary will be rounded to the nearest integer

42 Select Statements Mathematical Functions cont’d Before After

43 Select Statement Table Joins All the querying you have seen so far has been for one table, Joins will break the barriers holding you back Joining allows you to relate multiple tables to each other which is the reason the databases you are using are called relational.

44 Select Statements Table Joins cont’d Here is the BASIC syntax for a join: Select "columnname" from "tablename", "tablename2" where "condition"

45 Select Statements Table Joins cont’d Joins can be explained easier by demonstrating what would happen if you worked with one table only, and didn't have the ability to use "joins". This single table database is also sometimes referred to as a "flat table". Let's say you have a one-table database that is used to keep track of all of your customers and what they purchase from your store

46 Select Statements Table Joins cont’d Everytime a new row is inserted into the table, all columns will be be updated, thus resulting in unnecessary "redundant data". For example, every time Wolfgang Schultz purchases something, the following rows will be inserted into the table:

47 Select Statements Table Joins cont’d An ideal database would have two tables: 1) One for keeping track of your customers 2) And the other to keep track of what they purchase: "Customer_info" table: customer_number|firstname|lastname|address|city|state|zip| "Purchases" table: customer_number|date|item|price|

48 Select Statements Table Joins cont’d Now, whenever a purchase is made from a repeating customer, the 2nd table, "Purchases" only needs to be updated! We've just eliminated useless redundant data, that is, we've just normalized this database!

49 Select Statements Table Joins cont’d Normalization - Data Normalization is a database design technique that is used to get the tables in your database into at least the third normal form (3NF). Basically, this means that you want to eliminate the redundancy of non-key data when constructing your tables. Each table should only have columns that depend on the primary key.

50 Select Statements Table Joins cont’d Notice how each of the tables have a common "cusomer_number" column. This column, which contains the unique customer number will be used to JOIN the two tables. Using the two new tables, let's say you would like to select the customer's name, and items they've purchased. Here is an example of a join statement to accomplish this: SELECT customer_info.firstname, customer_info.lastname, purchases.item FROM customer_info, purchases WHERE customer_info.customer_number = purchases.customer_number;

51 Select Statements Table Joins cont’d This particular "Join" is known as an "Inner Join" or "Equijoin". This is the most common type of "Join" that you will see or use.Notice that each of the colums are always preceeded with the table name and a period. This isn't always required, however, it IS good practice so that you wont confuse which colums go with what tables. It is required if the name column names are the same between the two tables. I recommend preceeding all of your columns with the table names when using joins.

52 Select Statements Table Joins cont’d example: SELECT employee_info.employeeid, employee_info.lastname, employee_sales.comission FROM employee_info, employee_sales WHERE employee_info.employeeid = employee_sales.employeeid; This statement will select the employeeid, lastname (from the employee_info table), and the comission value (from the employee_sales table) for all of the rows where the employeeid in the employee_info table matches the employeeid in the employee_sales table.


Download ppt "SQL Intermediate Workshop Authored by Jay Mussan-Levy."

Similar presentations


Ads by Google