0% found this document useful (0 votes)
3 views1 page

3.PracticalSQL2E SampleCh3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

3.PracticalSQL2E SampleCh3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Practical SQL 2e (Sample Chapter) © 2021 by Anthony DeBarros

Note that the id column (of type bigserial) is automatically filled with
sequential integers, even though you didn’t explicitly insert them. Very
handy. This auto-incrementing integer acts as a unique identifier, or key,
that not only ensures each row in the table is unique, but also later gives us
a way to connect this table to other tables in the database.
Before we move on, note that you have two other ways to view all rows
in a table. Using pgAdmin, you can right-click the teachers table in the
object tree and choose View/Edit DataAll Rows. Or you can use a little-
known bit of standard SQL:

TABLE teachers;

Both provide the same result as the code in Listing 3-1. Now, let’s refine
this query to make it more specific.

Querying a Subset of Columns


Often, it’s more practical to limit the columns the query retrieves, especially
with large databases, so you don’t have to wade through excess information.
You can do this by naming columns, separated by commas, right after the
SELECT keyword. Here’s an example:

SELECT some_column, another_column, amazing_column FROM table_name;

With that syntax, the query will retrieve all rows from just those three
columns.
Let’s apply this to the teachers table. Perhaps in your analysis you want
to focus on teachers’ names and salaries, not the school where they work
or when they were hired. In that case, you would select just the relevant
columns. Enter the statement shown in Listing 3-2. Notice that the order of
the columns in the query is different than the order in the table: you’re able
to retrieve columns in any order you’d like.

SELECT last_name, first_name, salary FROM teachers;

Listing 3-2: Querying a subset of columns

Now, in the result set, you’ve limited the columns to three:

last_name first_name salary


--------- ---------- ------
Smith Janet 36200
Reynolds Lee 65000
Cole Samuel 43500
Bush Samantha 36200
Diaz Betty 43500
Roush Kathleen 38500

Although these examples are basic, they illustrate a good strategy for
beginning your interview of a dataset. Generally, it’s wise to start your
analysis by checking whether your data is present and in the format you

Beginning Data Exploration with SELECT 3

You might also like