Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 39
Basics of Mysql
MySql Select • The SELECT statement in MySQL is used to retrieve data from one or more tables in a database.
• It is one of the most commonly used SQL
commands Syntax of SELECT Statement SELECT column1, column2, ..., columnN FROM table_name WHERE condition ORDER BY column_name [ASC|DESC] LIMIT number; Key Components • SELECT: Specifies the columns you want to retrieve. • FROM: Specifies the table from which to retrieve the data. • WHERE: Filters the rows that satisfy a specified condition (optional). • ORDER BY: Sorts the result by one or more columns (optional). • LIMIT: Limits the number of rows returned by the query (optional) MySql Update • The UPDATE statement in MySQL is used to modify existing records in a table.
• It allows you to change the values of one or
more columns for rows that match a specified condition Syntax of UPDATE Statement UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; Basic UPDATE Example: UPDATE projects SET name = 'Updated Project', description = 'This is an updated description' WHERE id = 1; MySQL DELETE Statement • The DELETE statement in MySQL is used to remove rows from a table.
• You can delete specific rows by using the
WHERE clause, or you can delete all rows from a table if no condition is provided Syntax of DELETE Statement • DELETE FROM table_name • WHERE condition; MySQL TRUNCATE Statement • The TRUNCATE statement in MySQL is used to quickly remove all rows from a table.
• It is different from DELETE because TRUNCATE
is more efficient for emptying tables and resets the auto-increment counter. Syntax of TRUNCATE Statement • TRUNCATE TABLE table_name; Key Differences Between DELETE and TRUNCATE
• Use DELETE:When you want to remove
specific rows from a table. • When you need to retain control over the deletion process, especially when using transactions. • When you want to respect foreign key constraints. • Use TRUNCATE: • When you want to quickly remove all rows from a table. • When resetting the auto-increment counter is desirable. • When you don’t need to log individual row deletions or use transactions • DELETE: Removes rows one by one and can delete specific rows when used with the WHERE clause. • TRUNCATE: Removes all rows at once, without row-by-row deletion, making it faster Where Condition • The WHERE clause in SQL is used to filter records in a database based on specific conditions. • It allows you to specify which rows should be included in the result set by applying one or more conditions. • Without the WHERE clause, all rows from the queried table would be returned, which may not be what you need, especially when working with large datasets syntax SELECT column1, column2, ... FROM table_name WHERE condition; For e.g. (1)SELECT * FROM fruits WHERE fruit_name = 'Apple’; (2) SELECT * FROM fruits WHERE color = 'Red' AND price < 15; A complex One SELECT f.fruit_name, s.supplier_name FROM fruits f JOIN suppliers s ON f.supplier_id = s.id WHERE s.country = 'Brazil'; ORDER BY Clause • The ORDER BY clause is used to sort the result set in either ascending (ASC) or descending (DESC) order based on one or more columns Example SELECT fruit_name, price FROM fruits ORDER BY price ASC; • This query retrieves the list of fruits, ordered by their price in ascending order (lowest to highest). By default, if you omit ASC, it will still sort in ascending order Example 2 SELECT fruit_name, price, quantity FROM fruits ORDER BY price ASC, quantity DESC;
• This query sorts the result set first by price in
ascending order, and for rows with the same price, it further sorts by quantity in descending order. Group by clause: • The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". • The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns. Example SELECT fruit_name, COUNT(*) AS fruit_count FROM fruits GROUP BY fruit_name; • This query counts how many times each fruit appears in the table by grouping the result set by the fruit_name. Example 2 SELECT fruit_name, AVG(price) AS average_price FROM fruits GROUP BY fruit_name; • This query groups the result set by fruit_name and calculates the average price for each fruit Combining GROUP BY and ORDER BY • we can also combine both GROUP BY and ORDER BY in a single query to first group the data and then order the results based on the aggregated data Example SELECT color, SUM(quantity) AS total_quantity FROM fruits GROUP BY color ORDER BY total_quantity DESC; • This query groups the fruits by color, sums their quantity, and then orders the result set by the total quantity in descending order (from highest to lowest). Example 2 SELECT fruit_name, AVG(price) AS average_price FROM fruits GROUP BY fruit_name ORDER BY average_price ASC; • This query groups the fruits by fruit_name, calculates the average price for each fruit, and orders the result set by the average price in ascending order. Having clause • The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions. • Key Differences between Having and Where Clause
• WHERE is used to filter records before any
grouping or aggregation is done. • HAVING is used to filter groups of records after applying aggregate functions. syntax • SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s); Example 1 SELECT category, SUM(total_sales) AS total_sales FROM sales GROUP BY category HAVING SUM(total_sales) > 1000; • In this query: • The GROUP BY clause groups the results by category. • The HAVING clause filters only those categories where the total sales exceed 1000 Using HAVING with COUNT() SELECT course_name, COUNT(student_id) AS student_count FROM students GROUP BY course_name HAVING COUNT(student_id) > 5; • This query counts how many students are enrolled in each course and returns only those courses with more than 5 students. Like operator • The MySQL LIKE Operator • The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. • There are two wildcards often used in conjunction with the LIKE operator: • The percent sign (%) represents zero, one, or multiple characters • The underscore sign (_) represents one, single character syntax • SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern; • Example: • SELECT * FROM Customers WHERE CustomerName LIKE 'a%'; Aggregate functions • Count function:The COUNT() function returns the number of rows that matches a specified criterion. • SELECT COUNT(column_name) FROM table_name WHERE condition; Sum function • The SUM() function returns the total sum of a numeric column • SELECT SUM(column_name) FROM table_name WHERE condition; Average function • The AVG() function returns the average value of a numeric column. • SELECT AVG(column_name) FROM table_name WHERE condition; Min and max function • The MIN() function returns the smallest value of the selected column. • The MAX() function returns the largest value of the selected column. • MIN() Syntax:SELECT MIN(column_name) FROM table_name WHERE condition; • Max syntax:SELECT MAX(column_name) FROM table_name WHERE condition;