How to Select a Subset in MySQL
You can select a subset of data from a SELECT statement using MySQL (My Standard Query Language). MySQL permits programmers to return a subset of data based on almost any imaginable criteria. For example, you may wish to return only data from a specific column in a table. You might want to return only the "customer name" value from a customer table, while keeping the customer's credit card number and address hidden. Or you might wish to return only "redheads" from a dating database that allows customers to search for people to date based on their hair color.
Instructions
-
-
1
Open the MySQL development environment that you prefer and create a new query file.
-
2
Type "SELECT" at the top of the file:
SELECT
-
-
3
Use the "FROM" keyword to restrict the data returned from a "SELECT" statement to only include information from a particular table:
SELECT
[FROM Customers
-
4
Use a "WHERE" statement to extract data by restricting values to those that meet a specific, defined criteria:
SELECT
[FROM Customers
[WHERE customer = Sarah
-
5
Combine multiple "WHERE" statements using the "AND" keyword. The following code allows you to return the subset of all customers named Sarah who listed California as their home state:
SELECT
[FROM Customers
[WHERE customer = "Sarah" AND home_state = "California"
-
6
Save the query and execute it to ensure it functions as expected.
-
1