The document provides an overview of various MySQL clauses including WHERE, DISTINCT, FROM, ORDER BY, GROUP BY, and HAVING, detailing their syntax and usage. It explains how to filter results, remove duplicates, join tables, sort records, and group data with aggregate functions. Examples are included for each clause to illustrate their application in SQL queries.
The document provides an overview of various MySQL clauses including WHERE, DISTINCT, FROM, ORDER BY, GROUP BY, and HAVING, detailing their syntax and usage. It explains how to filter results, remove duplicates, join tables, sort records, and group data with aggregate functions. Examples are included for each clause to illustrate their application in SQL queries.
MySQL WHERE Clause is used with SELECT, INSERT, UPDATE and DELETE clause to filter the results. It specifies a specific position where you have to do the operation. Syntax: WHERE conditions; Parameter: conditions: It specifies the conditions that must be fulfilled for records to be selected. MySQL WHERE Clause with AND condition In this example, we are retrieving data from the table "officers" with AND condition. Execute the following query: SELECT * FROM officers WHERE address = 'Lucknow' AND officer_id < 5; WHERE Clause with OR condition Execute the following query: SELECT * FROM officers WHERE address = 'Lucknow' OR address = 'Mau'; MySQL WHERE Clause with combination of AND & OR conditions You can also use the AND & OR conditions altogether with the WHERE clause. See this example: Execute the following query: SELECT * FROM officers WHERE (address = 'Mau' AND officer_name = 'Ajeet') OR (officer_id < 5); MySQL Distinct Clause MySQL DISTINCT clause is used to remove duplicate records from the table and fetch only the unique records. The DISTINCT clause is only used with the SELECT statement. Syntax: SELECT DISTINCT expressions FROM tables [WHERE conditions]; Parameters expressions: specify the columns or calculations that you want to retrieve. tables: specify the name of the tables from where you retrieve records. There must be at least one table listed in the FROM clause. WHERE conditions: It is optional. It specifies the conditions that must be met for the records to be selected. Note: If you put only one expression in the DISTINCT clause, the query will return the unique values for that expression. If you put more than one expression in the DISTINCT clause, the query will retrieve unique combinations for the expressions listed. In MySQL, the DISTINCT clause doesn't ignore NULL values. So if you are using the DISTINCT clause in your SQL statement, your result set will include NULL as a distinct value. SELECT DISTINCT address FROM officers; MySQL DISTINCT Clause with multiple expressions If you use multiple expressions with DISTINCT Clause then MySQL DISTINCT clause will remove duplicates from more than one field in your SELECT statement. Use the following query: SELECT DISTINCT officer_name, address FROM officers; MySQL FROM Clause The MySQL FROM Clause is used to select some records from a table. It can also be used to retrieve records from multiple tables using JOIN condition. Syntax: FROM table1 [ { INNER JOIN | LEFT [OUTER] JOIN| RIGHT [OUTER] JOIN } table2 ON table1.column1 = table2.column1 ] Parameters table1 and table2: specify tables used in the MySQL statement. The two tables are joined based on table1.column1 = table2.column1. Note: If you are using the FROM clause in a MySQL statement then at least one table must have been selected. If you are using two or more tables in the MySQL FROM clause, these tables are generally joined using INNER or OUTER joins. MySQL FROM Clause: Retrieve data from one table The following query specifies how to retrieve data from a single table. Use the following Query: SELECT * FROM officers WHERE officer_id <= 3; MySQL FROM Clause: Retrieve data from two tables with inner join Let's take an example to retrieve data from two tables using INNER JOIN. Here, we have two tables "officers" and "students". Execute the following query: SELECT officers.officer_id, students.student_name FROM students INNER JOIN officers ON students.student_id = officers.officer_id; MySQL FROM Clause: Retrieve data from two tables using outer join Execute the following query: SELECT officers.officer_id, students.student_name FROM officers LEFT OUTER JOIN students ON officers.officer_id = students.student_id; MySQL ORDER BY Clause The MYSQL ORDER BY Clause is used to sort the records in ascending or descending order. Syntax: SELECT expressions FROM tables [WHERE conditions] ORDER BY expression [ ASC | DESC ]; Parameters expressions: It specifies the columns that you want to retrieve. tables: It specifies the tables, from where you want to retrieve records. There must be at least one table listed in the FROM clause. WHERE conditions: It is optional. It specifies conditions that must be fulfilled for the records to be selected. ASC: It is optional. It sorts the result set in ascending order by expression (default, if no modifier is provider). DESC: It is also optional. It sorts the result set in descending order by expression. MySQL ORDER BY: without using ASC/DESC attribute If you use MySQL ORDER BY clause without specifying the ASC and DESC modifier then by default you will get the result in ascending order. Execute the following query: SELECT * FROM officers WHERE address = 'Lucknow' ORDER BY officer_name; MySQL ORDER BY: with ASC attribute Let's take an example to retrieve the data in ascending order. Execute the following query: SELECT * FROM officers WHERE address = 'Lucknow' ORDER BY officer_name ASC; MySQL ORDER BY: with DESC attribute SELECT * FROM officers WHERE address = 'Lucknow' ORDER BY officer_name DESC; MySQL ORDER BY: using both ASC and DESC attributes Execute the following query: SELECT officer_name, address FROM officers WHERE officer_id < 5 ORDER BY officer_name DESC, address ASC; MySQL GROUP BY Clause The MYSQL GROUP BY Clause is used to collect data from multiple records and group the result by one or more column. It is generally used in a SELECT statement. You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. on the grouped column. Syntax: SELECT expression1, expression2, ... expression_n, aggregate_function (expression) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expression_n; Parameters expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY clause. aggregate_function: It specifies a function such as SUM, COUNT, MIN, MAX, or AVG etc. tables: It specifies the tables, from where you want to retrieve the records. There must be at least one table listed in the FROM clause. WHERE conditions: It is optional. It specifies the conditions that must be fulfilled for the records to be selected. (i) MySQL GROUP BY Clause with COUNT function Consider a table named "officers" table, having the following records. Execute the following query: SELECT address, COUNT(*) FROM officers GROUP BY address; MySQL HAVING Clause MySQL HAVING Clause is used with GROUP BY clause. It always returns the rows where condition is TRUE. Syntax: SELECT expression1, expression2, ... expression_n, aggregate_function (expression) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expression_n HAVING condition; Parameters aggregate_function: It specifies any one of the aggregate function such as SUM, COUNT, MIN, MAX, or AVG. expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY clause. WHERE conditions: It is optional. It specifies the conditions for the records to be selected. HAVING condition: It is used to restrict the groups of returned rows. It shows only those groups in result set whose conditions are TRUE. HAVING Clause with SUM function Consider a table "employees" table having the following data. Execute the following query: SELECT emp_name, SUM(working_hours) AS "Total working hours" FROM employees GROUP BY emp_name HAVING SUM(working_hours) > 5;