One of the most widely used relational database management systems (RDBMS) worldwide is MySQL. Millions of developers and companies use it to manage and store data. Although MySQL is a strong and flexible database, learning it can be challenging for novices.
We’ll go over the 10 crucial MySQL queries that each student and developer has to be familiar with. You can use these queries to carry out both simple and complex database actions, including selecting, inserting, updating, and deleting data.
1. Select all data from a table
This query will return all of the data in a table.
SELECT * FROM customers;
2. Select specific columns from a table
This query will return only the specified columns from a table.
SELECT name, email FROM customers;
3. Select data from a table with conditions
This query will return only the rows from a table that match the specified conditions.
SELECT * FROM customers WHERE country = 'United States';
4. Order data by a column
This query will return the rows from a table in a specific order, such as ascending or descending by a column.
SELECT * FROM customers ORDER BY name ASC;
5. Group data by a column
This query will return the rows from a table in a specific order, such as ascending or descending by a column.
SELECT country, COUNT(*) AS count FROM customers GROUP BY country;
6. Aggregate data using functions such as SUM, AVG, and COUNT
This query will calculate an aggregate value, such as the sum or average, for a column in a table.
SELECT SUM(total_spent) AS total_revenue FROM orders;
7. Join two tables
This query will combine the data from two tables based on a common field.
SELECT customers.name, orders.total_spent
FROM customers
INNER JOIN orders ON customers.id = orders.customer_id;
8. Update data in a table
This query will update the data in a table based on a specified condition.
UPDATE customers SET email = 'new_email@example.com' WHERE id = 1;
9. Delete data from a table
This query will delete the data from a table based on a specified condition.
DELETE FROM customers WHERE id = 1;
10. Create a new table
This query will create a new table with the specified columns and data types.
CREATE TABLE products (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL,
PRIMARY KEY (id)
);
MySQL is a powerful and versatile database, and these 10 essential queries will give you the foundation you need to start using it effectively. Whether you’re a beginner or a seasoned developer, these queries are a must-know.