A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.
Launch Xperto-AIWhen working with databases, SQL (Structured Query Language) is a powerful tool for handling data. MySQL, a popular open-source relational database management system, utilizes SQL for its queries. This blog post highlights some fundamental SQL queries that you can use for data retrieval in MySQL.
The cornerstone of data retrieval in SQL is the SELECT
statement. This command allows you to specify which columns of data you want to fetch from a table.
SELECT column1, column2, ... FROM table_name;
Suppose we have a table named employees
:
id | name | position | salary |
---|---|---|---|
1 | Alice | Manager | 70000 |
2 | Bob | Developer | 60000 |
3 | Charlie | Designer | 55000 |
To retrieve all employee names and their positions, you would write:
SELECT name, position FROM employees;
name | position |
---|---|
Alice | Manager |
Bob | Developer |
Charlie | Designer |
If you want to retrieve all columns from a table, you can use the asterisk (*
) wildcard.
SELECT * FROM table_name;
To retrieve all the information from the employees
table, the query would look like this:
SELECT * FROM employees;
id | name | position | salary |
---|---|---|---|
1 | Alice | Manager | 70000 |
2 | Bob | Developer | 60000 |
3 | Charlie | Designer | 55000 |
The WHERE
clause adds a condition to your query, allowing you to filter the results based on specific criteria.
SELECT column1, column2, ... FROM table_name WHERE condition;
To find employees with a salary greater than 60000, you would write:
SELECT name, salary FROM employees WHERE salary > 60000;
name | salary |
---|---|
Alice | 70000 |
Bob | 60000 |
You can combine multiple conditions using the AND
and OR
operators.
SELECT column1, column2, ... FROM table_name WHERE condition1 AND/OR condition2;
To find employees with a salary greater than 60000 but who are not Developers:
SELECT name, position, salary FROM employees WHERE salary > 60000 AND position <> 'Developer';
name | position | salary |
---|---|---|
Alice | Manager | 70000 |
To arrange your results, you can use the ORDER BY
clause, which sorts the output by specified column(s).
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC|DESC];
To fetch all employees sorted by their salary in descending order:
SELECT name, salary FROM employees ORDER BY salary DESC;
name | salary |
---|---|
Alice | 70000 |
Bob | 60000 |
Charlie | 55000 |
When you want to limit the number of records returned, you can use the LIMIT
clause.
SELECT column1, column2, ... FROM table_name LIMIT number;
To fetch only the top 2 highest-paid employees:
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 2;
name | salary |
---|---|
Alice | 70000 |
Bob | 60000 |
To retrieve unique values from a particular column, the DISTINCT
keyword can be very useful.
SELECT DISTINCT column1 FROM table_name;
If you wanted to list all unique positions in the employees
table:
SELECT DISTINCT position FROM employees;
position |
---|
Manager |
Developer |
Designer |
These basic SQL queries are just the tip of the iceberg when it comes to working with MySQL, but they form the essential toolkit for data retrieval. Understanding and using these commands will vastly improve your ability to interact with databases and extract valuable information. As you continue your journey, you’ll find that these basic queries can be combined and expanded to create powerful data retrieval techniques.
09/11/2024 | MySQL
09/11/2024 | MySQL
09/11/2024 | MySQL
09/11/2024 | MySQL
09/11/2024 | MySQL
09/11/2024 | MySQL
09/11/2024 | MySQL
09/11/2024 | MySQL