So, we have created your database, added data into the tables and now what? In real world nobody collects data for the sake of collecting something. It has to be something that they can use at a later time. This section is dedicated to retrieving information stored in a database. To get the information, you use the SELECT command. Here is the syntax for using the select command:
SELECT firstname, lastname,gender FROM customer;
To retrieve data, you use the keyword SELECT followed by the columns whose data you would like to retrieve. That is followed by the keyword FROM after which you can specify the table from which you would like to get the data. Alternatively, If you wanted to retrieve values in all columns, you would use the following syntax:
SELECT * FROM customer;
In SQL terms, * means all values.
Ordering the Retrieved Values
To order retrieved values, you use the phrase Order by in your select statement. Bellow is how you do it:
SELECT * FROM customer ORDER BY firstname;
This statement will return all values in the table ordered in ascending order on firstname field. This means every record will be returned in Alphabetical order sorted by first names. The default order is ascending, if you wanted to order the results in reverse order, you use the DESC phrase. Here is how you do it:
SELECT * FROM customer ORDER BY firstname DESC;
This will still return the same results, only sorted in reverse order. This means customers with first names starting with Z will come before those whose first names starts with A.
Limiting the results returned
There are several ways to limit the results returned by your SQL Query. One of the ways is to use the WHERE clause to limit your results to only records that meet a specified criteria. Another method is to use the key phrase LIMIT to actually limit the number of records returned to a number that you specify. We will be discussing each approach in detail.
Using the WHERE clause to limit SQL query results:
To use this approach, you will have to specify the conditions to be met by the returned records, if no record meets the condition(s), nothing will be returned. Here is an example of how to use the WHERE clause with your select statement:
SELECT * FROM customer WHERE lastname = ‘Makushe’ ORDER BY firstname;
This query would only return records with customers whose last name is ‘Makushe’ and such a list of records is ordered by firstnames:
Using the LIMIT clause to limit SQL query results:
Using this approach all you really have to do is order the list of results and use the LIMIT keyword to specify the number of records you would like to be returned. Bellow is the syntax:
SELECT * FROM customer ORDER BY firstname LIMIT 2;
This statement tells the server to return all field values in the table customers, but there is a twist to it, this query will sort through the results order the records in ascending order by first name and only return the first two.
This kind of a query is useful in cases where you are required to get the top N or bottom N of a collection. For example if you were dealing with test scores for your students and want to select the top five scorers, you would select all their results as illustrated above order the results in descending order and limit the results to the top 5.
|