Login | Contact Us | Site Map | Resources

Data Manipulation

Home > MYSQL Tutorial > Selecting MYSQL Data

Selecting MYSQL Data

Now that you have inserted data into your database, you will at some point in time want to access that data. This can be done by "selecting" the data that you want.

SELECT * FROM pets;

This simple example can be read as "select all from pets", and will print out onto your screen every single record stored in the pets database. The asterisk is a wildcard (meaning "all") that allows you to be less selective in your selection.

In order to be more selective in your selection, the "where" clause can be used. It allows you to specify requirements that a record (or multiple records) must meet before they can be returned.

SELECT * FROM pets WHERE name LIKE 'Tamika';

Here we have a query that can be read as "select all from pets where name is like Tamika". Instead of sending you all of the records in the pets table, this statement will only return record(s) in the pets table where "Tamika" is stored in the name column. (Bonus: Did you recognize the operator that we used in this query?)

This leaves us with one simple problem. The WHERE clause will look for an identical match to the word(s)/phrase(s)/character(s) that are specified. What if you only know part of the data that you are searching for? The % wildcard will allow you to search for partial matches.

SELECT * FROM pets WHERE breed LIKE '%Ferret%';

In this example we see our search term surrounded by the % wildcard. This means that all records that contain "Ferret" in the breed column will be returned, even if they contain additional characters before and/or after the word "Ferret". ("Black-Footed Ferret", "Ferret" and "Ferret Weasel" will now all be valid matches.)

If the wildcard is only used on one side of the search term, then additional characters are only allowed to that one side.

Results are generally not case-sensitive, so searching for "ferret" or "tamika" will turn up the record just as well as searching for "Ferret" or "Tamika".

If you only need specific columns returned, you can specify which column (or columns) in place of using the * asterisk wildcard. Multiple column names should be comma separated.

SELECT name, gender, description FROM pets;