Introduction to SELECT in MySQL
In this topic, we are going to learn about SELECT in MySQL and mostly into DQL which is “Data Query Language”. This comes to play when we try to fetch records from the database and it starts with the “SELECT” command. This command can be used with many SQL clauses as well as other functions to get desired records.
There are many kinds of SQL commands which can be categorized into the following:
- DDL (Data definition language)
- DML (Data manipulation language)
- DQL (Data query language)
- DCL (Data control language)
- TCL (Transaction control language)
1. The basic syntax of the SELECT command:
SELECT * FROM table_name;
This will fetch all the records with all attributes from a table.
SELECT column1, column2,…. FROM table_name;
This will fetch specified columns from a table that are passed through the query.
2. This SELECT command can also be used with the INSERT command which is used for adding records to the existing table.
INSERT INTO table_name1 SELECT * FROM table_name2;
Here query will fetch all the records from table_name2 and will insert those into table_name1.
Examples to Implement SELECT in MySQL
Let’s consider there is a customer table with the following attributes.
We are going to see some basic SELECT queries using some clauses for the basic understanding of how this command works.
SELECT * FROM customer;
(This will fetch all the records with all attributes from a table.)
SELECT cust_id, first_name, last_name, email,city FROM customer;
(This will fetch specified columns from a table which are passed through a query)
SELECT cust_id, first_name, last_name, email,city FROM customer WHERE city = 'Delhi';
(WHERE command will fetch those records only, where the city will be ‘Delhi’)
SELECT cust_id, first_name, last_name,city, amount FROM customer WHERE amount BETWEEN 5000 AND 25000;
(BETWEEN clause will return records which satisfy the given range of condition passed in the query)
SELECT * FROM customer ORDER BY amount DESC;
(Used to sort both numeric and string values either in ascending or descending manner. But by default, it sorts in ascending manner. If we want in descending, then we need to specify it after using ORDER BY clause)
Clauses with SELECT Command
Other clauses with SELECT command:
1. SELECT: Used to fetch all the records from a table.
SELECT * FROM table;
2. DISTINCT: Used to fetch all the unique values from a table.
SELECT DISTINCT col_name FROM table;
3. WHERE: Used forgiving conditions in the retrieval of records.
SELECT employee_id FROM employee WHERE name = 'stella';
4. COUNT: Used to get the number of records present in a table.
SELECT COUNT(*) FROM employee;
5. ORDER BY: Used to sort both numeric and string values either in ascending or descending manner. But by default, it sorts in ascending manner. If we want to descend, then we need to specify it after using the ORDER BY clause.
SELECT first_name FROM student ORDER BY marks desc;
6. LIMIT: This is used to specify the number of records we want after executing the query. If we want the top 5 students of a class, then after sorting the results, we can use this LIMIT by specifying 5. So that it will only fetch the top 5 records.
SELECT first_name FROM student ORDER BY marks desc LIMIT 5;
(**ORDER BY used here for sorting value in descending order)
7. AND: If 2 conditions are given and both are met for a record, then only the query will fetch that records.
SELECT employee_id FROM employee WHERE name = 'stella' AND city = 'Bangalore';
8. OR: If 2 conditions are given and one of them is met for a record, then that record will be fetched.
SELECT employee_id FROM employee WHERE department = 'IT' OR city = 'Bangalore';
9. NOT: Used with conditions. If we specify NOT before any conditions, records that are not meeting that conditions will be fetched.
SELECT employee_id FROM employee WHERE NOT BETWEEN 1 AND 10;
10. BETWEEN: This operator selects records within a given range. Mostly we use this where we want to specify a range of dates.
SELECT emp_id FROM employee WHERE emp_id BETWEEN 1 AND 10;
SELECT * FROM employee WHERE join_date BETWEEN '2007-01-01' AND '2008-01-01';
11. IN: This operator allows us to specify multiple values in a WHERE clause.
SELECT * FROM employee WHERE employee_id IN (1001,1004,1008,1012);
12. LIKE: This operator is used with the WHERE clause to search for a specified pattern in a column containing the string.
- ‘A%’ – string starts with A
- ‘&A’ – ends with A
- ‘%A%’ – A will be in between the string
- ‘_A%’ – Here the 2nd letter will be A
- ‘%A_’ – The 2nd from the last letter will be A
SELECT first_name FROM table WHERE first_name LIKE 'A%';
13. SUBSTRING: Used to pick a specific character from a string by specifying the position.
SELECT SUBSTRING(customer_name,1,5) FROM customer_table;
(it will fetch character from 1st to 5th position of a string)
14. INSTR: This returns a position of a string in another string.
SELECT INSTR('independence', 'pen');
(it will find the position of ‘pen’ in the word ‘independence’)
15. GROUP BY: This is used to segregate records on the basis of some given conditions.
SELECT employee_id FROM employee GROUP BY department HAVING salary > 100000;
(Here group by segregated employees on the basis of their department and whose salary more than 100k.
Condition always comes with HAVING statement in GROUP BY clause.)
Below is the different aggregate function:
1. SUM: Calculates the sum of values.
SELECT SUM(salary) FROM employee;
2. AVG: Calculates the average set of values.
SELECT AVG(salary) FROM employee;
3. MIN: Gets the minimum value in a set of values.
SELECT MIN(salary) FROM employee;
4. MAX: Gets the maximum value in a set of values.
SELECT MAX(salary) FROM employee;
Joins in SELECT in MySQL
1. INNER JOIN: Returns records that have a matching value in both tables.
SELECT * FROM order INNER JOIN customer ON order.cust_id = customer.cust_id;
2. LEFT JOIN: Returns all records from the left table and the matched records from the right table.
SELECT * FROM order LEFT JOIN customer ON order.cust_id = customer.cust_id;
3. RIGHT JOIN: Returns all records from the right table and the matched records from the left table.
SELECT * FROM order RIGHT JOIN customer ON order.cust_id = customer.cust_id;
4. FULL OUTER JOIN: Returns all the records when there is a match on either the left or the right table.
SELECT * FROM order FULL OUTER JOIN customer ON order.cust_id = customer.cust_id;
These commands and clauses we have discussed above are very useful in real-time scenarios as it provides the basic concepts of how to use SQL queries to fetch and manipulate data in the database. Apart from this, while using advance and analytical queries like window function etc, these clauses are very important.
This is a guide to SELECT in MySQL Here we discuss the Example to implement SELECT in MySQL with Clauses, Aggregate Functions, and Joins. You can also go through our other suggested articles to learn more –