Chapter 7. Constructing Simple Database Queries
In most databases, the vast majority of SQL statements issued are designed to retrieve information from a database. You can use the SQL SELECT
statement to retrieve information from database tables. The beauty of this statement is that it's quite simple to use in its basic form, but it also contains quite a bit of flexible power, allowing you to precisely specify the exact information you'd like to retrieve.
In this chapter, I dissect the SELECT
statement, clause-by-clause, and show you how to put together simple database queries. I recommend that you master this material before you check out the more powerful uses of the SELECT
statement that I present in Chapter 8.
Retrieving Data with SELECT Statements
The SQL command used to retrieve data from a database is the SELECT
statement. As do other SQL statements, the SELECT
statement reads almost like an English statement. If you can fill in the blanks in the following sentence, you can compose a SELECT
statement:
Select ____columns____ from ___table___ where ____ conditions____.
That's really all there is to it. You simply need to identify three things to compose a proper SELECT
query:
The columns you want to retrieve
The table you want to retrieve them from
The conditions (if any) that the data must satisfy
You then take this information and plug it into the proper SQL syntax. For example, suppose you wanted to retrieve a list of students from a school database to determine which students might ...
Get Microsoft® SQL Server® 2008 For Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.