MySql: Queries


Published on

MySql: Queries

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MySql: Queries

  2. 2. OVERVIEW<br />Querying data using joins<br />Aggregation functions<br />Creating and using views<br />Using union operator for querying<br />Subqueries.<br />
  3. 3. Querying data using joins<br />1.INNER JOIN()<br />CROSS JOIN and JOIN types are all similar to INNER JOIN<br />Used to link a table to itself.<br />Ex: consider two tables t1 and t2. <br />
  4. 4. Mysql&gt;select * from t1 inner join t2;<br />(here each row of one table is combined with each row of the other table, this join performs Cartesian product)<br />
  5. 5. (in order to perform inner join based on certain criteria use WHERE clause)<br />Mysql&gt;select t1.*,t2.* from t1 inner join t2 where t1.i1=t2.i2;<br /> <br />(inner join only selects the records which match the condition) <br />
  6. 6. OUTER JOINS<br />(these select even the records which donot satisfy the condition and place a NULL)<br />1.LEFT JOIN<br />( this forces the result set to contain a row for every entry selected from the left table, so if there is no match on the right table, we will find NULL in the result set)<br />Mysql&gt;select t1.*,t2.*<br /> from t1 left join t2<br /> on t1.i1=t2.i2;<br />
  7. 7. 2.RIGHT JOIN()<br />(this forces the result set to contain a row for every entry selected from the right table, so if there is no match on the left table, we will find NULL)<br />Mysql&gt;select t1.*,t2.* from t1 right join t2 on t1.i1=t2.i2;<br />Mysql&gt;select t1.* from t1 left join t2 on t1.i1=t2.i2<br />Where t1.i1 ISNULL;<br /> (left join is used to find the records where there is mismatch)<br />
  8. 8. Join can be used for self referencing<br />Ex: consider the emp_mgr table<br />
  9. 9. (employee table can be retrieved separately by<br />performing self join) employee_table<br />Mysql&gt;select t1.ssn, as<br /> e_name from emp_mgr as t1<br /> inner join emp_mgr as t2 on<br /> t2.ssn=t1.mgr_ssn; <br />
  10. 10. USING UNION FOR QUERYING<br />We can list the combined result set from different<br />tables.<br />Ex://suppose we have 2 teams one for quiz and 1 for debate as follows<br />quiz teamdebate team<br />
  11. 11. Mysql&gt;select name,age from quiz<br /> union<br /> select name,age from debate;<br /><br />(advantage of union: unique values are not repeated, it eleminates duplicate rows by default.<br />To preserve the duplicate rows use UNION ALL)<br />
  12. 12. Aggregate functions<br /> used to perform calculations on a group of<br />records and return a single value.<br />Often used with GROUP BY clause of SELECT<br />statement.<br />Some of the aggregate functions are<br />SUM,AVG,MIN,MAX,COUNT etc<br />
  13. 13. Ex:using employee database<br />Mysql&gt;select * from employee;<br />
  14. 14. 1.SUM()<br />Returns the sum of the grouped values, Returns 0 if there are no matching values.<br />Mysql&gt;select dept,sum(sal)<br />From employee<br />Group by dept; <br />2.AVG()<br />Returns the average of the grouped values.<br />Mysql&gt;select avg(sal) average_salary <br />from employee; <br />
  15. 15. 3.MIN() and MAX()<br />Mysql&gt;select min(sal) MIN, <br /> max(sal) MAX from employee;<br />4.COUNT()<br />Returns the count of values, here we can use to count the number of employee’s working in the company.<br />Mysq&gt;select count(*) from <br />Employee; <br />
  16. 16. 5.VARIANCE()<br />Mysql&gt;select variance(sal) from employee;<br />688888888.8889<br />6.STDDEV()<br />Mysql&gt;select stddev(sal) from employee;<br />26246.6929<br />
  17. 17. views<br />Views are virtual tables defined in terms of other(base) tables.<br />When the data in the underlying table changes so does<br />the data in the view.<br />But if the definitions in the table changes for ex: if we<br />add new columns to the table then these changes <br />might not apply for the views created.<br />
  18. 18. Views are updatable which means you can change the underlying table by means of operations on the view only when<br />the view is mapped directly onto a single table<br />view must select only the columns that are simple references to the table columns<br />any operation on the view row must correspond to an operation on a single in the underlying table.<br />no union operators,GROUP BY OR HAVING clauses,aggregate functions are used in the query.<br />
  19. 19. A simple view is a way to select a subset of a tables columns.<br />Syntax: CREATE VIEW view_name as select_query <br /> check condition;<br />Ex:using employee database<br />if we want to only select the columns name and ssn from the employee table we have to create the view.<br />Mysql&gt; create view vemp as select ssn,name from employee;<br />
  20. 20. Mysql&gt;select * from vemp;<br />We can further get data from the view by querying using WHERE,ORDER BY and other clauses.<br />You can provide column names explicitly by providing the names in closed parenthesis.<br />Mysql&gt; create view vemp (reg,enm) as<br /> select ssn,name from employee;<br />
  21. 21. A view can be used to perform calculations automatically.<br />Mysql&gt;create view exp as select name, timestampdiff(year,date-of-join,’2010-01-01’) as experience from employee;<br />Mysql&gt;select * from exp;<br />View can be defined on multiple tables, which makes it easy to use queries having join conditions.<br />
  22. 22. The select condition used to create the view can be altered using the syntax:<br />ALTER VIEW view_name select_condition<br />Check_condition;<br />The select query used to define the view can be retrieved using the syntax:<br />SHOW CREATE VIEW view_name;<br />To drop the view use the syntax:<br />DROP VIEW view_name;<br />
  23. 23. Setting privileges for the views<br />Privileges can be granted and revoked using any of the following:<br />GRANT CREATE VIEW;<br />GRANT SHOW CREATE VIEW;<br />REVOKE CREATE VIEW;<br />REVOKE SHOW CREATE VIEW;<br />
  24. 24. sub queries<br />Subqueries support one select query to be nested inside the other.<br />Subquery may return a single value,single column,single row or a table depending on the query.<br />Subqueries may be correlated or uncorrelated.<br />Subqueries can be tested using:<br />Comparison operators(=,&gt;,&lt;)<br />EXISTS,NOT EXISTS tests if the result of the subquery is empty.<br />IN,NOT IN tests if the value is present in the set returned by subquery.<br />ALL,ANY compare the value to the values returned.<br />
  25. 25. Subqueries with comparison operators<br />Comparison operators when used with the subqueries,will find all the rows in the outer query which satisfy the particular relationship with the value returned by the subquery.<br />Ex: to select the employee who is least paid we can use the subquery.<br />Mysql&gt;select * from e where <br />sal=(select min(sal) from e);<br />
  26. 26. Subqueries with IN and NOT IN<br />Used when the subquery returns multiple values.<br />Matches the row whose value is present in the set of values returned by the sub query.<br />Mysql&gt;select * from employee where ssn IN<br />(select mgr_ssn from manager);<br />retrieves the details of all employees who are managers<br />Mysql&gt;select * from employee where ssn NOT IN<br />(select mgr_ssn from manager);<br />retrieves the details of all employees who are not managers<br />
  27. 27. Subqueries with ALL and ANY <br />Used along with comparison operators.<br />ALL<br />Tests if the comparison value satisfies the particular<br />relationship with all the values returned by the<br />subquery.<br />Mysql&gt;select ssn,name from employee <br />where date_of_join &lt;= ALL<br />(select date_of_join from employee);<br />shows that jack is the most experienced working employee.<br />
  28. 28. ANY<br />Tests if the comparison value satisfies the<br />particular relationship with any value<br />returned by the subquery.<br />Mysql&gt;select ssn,name from <br />employee where date_of_join<br />&lt;= ANY(select date_of_join from employee);<br />this returns all the rows because every date_of_join is &lt;= atleast one of the other date including itself<br />
  29. 29. Subqueries with EXISTS and NOT EXISTS<br />Used to test if the subquery returns any row.<br />If it returns any row.EXISTS is true and NOT EXIST<br />is false, it returns 1.<br />Mysql&gt;select EXISTS(select * from employee where ssn=1111111);<br />0<br />Mysql&gt;select NOT EXISTS(select * from employee where ssn=1111111);<br />1<br />
  30. 30. Benefits sub queries<br /><ul><li>makes the query more </li></ul>Structured.<br /><ul><li>reduced data redundancy and </li></ul>Greater ease of manipulation.<br /><ul><li>sub queries are more readable than joins or unions, </li></ul>indeed it was the innovation of sub queries made people call the early SQL(“structured query language”). <br />
  31. 31. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at<br />