|Back to Help|
|2. A Simple Query|
|3. Common Searches|
|4. More Samples|
|5. Multiple Tables|
|6. Aggregate Fcns.|
|7. Group By|
|8. Order By|
More Sample Queries
Look at the following query:
The select and from blocks tell the database to look in the specObj table (which contains spectroscopic information) and return the redshift (z), the right ascension, the declination, and the (final, or ``best'') object ID. The ``where'' block tells the database to look only at galaxies with redshifts above 0.3 and no known problems with the redshift determination (zWarning = 0)
In this query, the word AND appears between all the characteristics in the where block. The query includes three characteristics - matching objects must be galaxies, they must have redshift > 0.3, and there must be no warnings about the redshift determination. All three of these characteristics must be met by a given record for the search to return that record.
AND is just one of three logical operators used by SQL; the others are OR and NOT. The meanings of the three logical operators are given in the table below:
You can combine the logical operators using parentheses. For example, for the characteristics A, B, and C, A AND (B OR C) means that either characteristics B or C, as well as characteristic A, must be met for records to match. What does A AND (NOT B) mean?
The query below is like the first query except that it will return both galaxies and quasars. The OR statement in the where block causes the query to return both.
Mathematical and Conditional Operators
SQL includes a variety of mathematical operators that perform math functions in a query. You can use multiplication, division, addition and subtraction. SQL uses the same symbols for these operators that most other computer languages use: + for addition, - for subtraction, * for multiplication, and / for division. Other mathematical operators include COS(x), which returns the cosine of column x, or SQRT(x), which returns the square root of column x. Here is a complete list of SQL's mathematical operators.
You can also use the conditional operators summarized below.
Let's say you wanted to find a list of very blue stars. In astronomy, color is defined by the differences in magnitudes (see the Color project for more information). In the SDSS's system of measurement, blue stars have u-g < 0.5. So to find blue stars, you might write a query like the following:
This query returns the first 10 photometric objects (in the PhotoObj table) that are labeled as stars (denoted by type = 6) where u - g < 0.5. You could further refine your query by searching for a certain ra and dec. Or you could limit yourself to bright stars by specifying a magnitude limit such as modelMag_r < 17.
Try pressing the buttons "Query 1," "Query 2," and "Query 3" below. These buttons will make the three queries above (in the purple boxes) appear in the query window. Press Submit to execute the queries. Write down a few of the ObjIDs, then use the Object Explorer to examine the objects. Do they have the characteristics you requested in the where block? What kinds of astronomy questions do you think you could answer by studying these objects?
Click Next to get some more practice in writing SQL queries.