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

More Related Content

What's hot

Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
Ehtisham Ali
 
Sql having clause
Sql having clauseSql having clause
Sql having clause
Vivek Singh
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
Hassen Poreya
 
Intro to tsql unit 15
Intro to tsql   unit 15Intro to tsql   unit 15
Intro to tsql unit 15
Syed Asrarali
 

What's hot (16)

SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
Correlated update vs merge
Correlated update vs mergeCorrelated update vs merge
Correlated update vs merge
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
 
Sql having clause
Sql having clauseSql having clause
Sql having clause
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
 
Intro to tsql unit 15
Intro to tsql   unit 15Intro to tsql   unit 15
Intro to tsql unit 15
 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part I
 
MYSql manage db
MYSql manage dbMYSql manage db
MYSql manage db
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-having
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
MYSQL using set operators
MYSQL using set operatorsMYSQL using set operators
MYSQL using set operators
 
Alter table command
Alter table commandAlter table command
Alter table command
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
 

Viewers also liked

Viewers also liked (16)

ТРИЗ И МЕДИАЦИЯ. Спор без желания смерти
ТРИЗ И МЕДИАЦИЯ. Спор без желания смертиТРИЗ И МЕДИАЦИЯ. Спор без желания смерти
ТРИЗ И МЕДИАЦИЯ. Спор без желания смерти
 
Medicina legal y_forense
Medicina legal y_forenseMedicina legal y_forense
Medicina legal y_forense
 
Diapositivas exposiciones
Diapositivas exposicionesDiapositivas exposiciones
Diapositivas exposiciones
 
PCM Aug 2014 updates
PCM Aug 2014 updatesPCM Aug 2014 updates
PCM Aug 2014 updates
 
Fostered Storyboard
Fostered StoryboardFostered Storyboard
Fostered Storyboard
 
Technology
TechnologyTechnology
Technology
 
Snr. Paralegal Diploma
Snr. Paralegal DiplomaSnr. Paralegal Diploma
Snr. Paralegal Diploma
 
From cosmos to intelligent life; the four ages of astrobiology
From cosmos to intelligent life; the four ages of astrobiologyFrom cosmos to intelligent life; the four ages of astrobiology
From cosmos to intelligent life; the four ages of astrobiology
 
Glucose Monitoring
Glucose MonitoringGlucose Monitoring
Glucose Monitoring
 
таничев константин+охранная организация+клиенты
таничев константин+охранная организация+клиентытаничев константин+охранная организация+клиенты
таничев константин+охранная организация+клиенты
 
yOS-tour Montreal - le bon, la brute et... Yammer
yOS-tour Montreal - le bon, la brute et... YammeryOS-tour Montreal - le bon, la brute et... Yammer
yOS-tour Montreal - le bon, la brute et... Yammer
 
ТРИЗ и медиация
ТРИЗ и медиацияТРИЗ и медиация
ТРИЗ и медиация
 
CEE Masterclass on Executive Leadership that Gets Results - 14 June 2013
CEE Masterclass on Executive Leadership that Gets Results - 14 June 2013CEE Masterclass on Executive Leadership that Gets Results - 14 June 2013
CEE Masterclass on Executive Leadership that Gets Results - 14 June 2013
 
Maqbool cv for Stoorkeeper
Maqbool cv for StoorkeeperMaqbool cv for Stoorkeeper
Maqbool cv for Stoorkeeper
 
Cracking an interview
Cracking an interviewCracking an interview
Cracking an interview
 
General quiz
General quizGeneral quiz
General quiz
 

Similar to MySQL Queries

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 
Twp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From OptimizerTwp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From Optimizer
qiw
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 

Similar to MySQL Queries (20)

sql statement
sql statementsql statement
sql statement
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
ADVANCED MODELLING.pptx
ADVANCED MODELLING.pptxADVANCED MODELLING.pptx
ADVANCED MODELLING.pptx
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Module03
Module03Module03
Module03
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 
Twp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From OptimizerTwp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From Optimizer
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Les18
Les18Les18
Les18
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

MySQL Queries

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