0% found this document useful (0 votes)
3 views

2.PracticalSQL2E_SampleCh3 - Copy

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 views

2.PracticalSQL2E_SampleCh3 - Copy

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

In SQL, interviewing data starts with the SELECT keyword, which


retrieves rows and columns from one or more of the tables in a database.
A SELECT statement can be simple, retrieving everything in a single table, or
it can be complex enough to link dozens of tables while handling multiple
calculations and filtering by exact criteria.
We’ll start with simple SELECT statements and then look into the more
powerful things SELECT can do.

Basic SELECT Syntax


Here’s a SELECT statement that fetches every row and column in a table called
my_table:

SELECT * FROM my_table;

This single line of code shows the most basic form of a SQL query. The
asterisk following the SELECT keyword is a wildcard, which is like a stand-in
for a value: it doesn’t represent anything in particular and instead repre-
sents everything that value could possibly be. Here, it’s shorthand for “select
all columns.” If you had given a column name instead of the wildcard,
this command would select the values in that column. The FROM keyword
indicates you want the query to return data from a particular table. The
semicolon after the table name tells PostgreSQL it’s the end of the query
statement.
Let’s use this SELECT statement with the asterisk wildcard on the teach-
ers table you created in Chapter 2. Once again, open pgAdmin, select the
analysis database, and open the Query Tool. Then execute the statement
shown in Listing 3-1. Remember, as an alternative to typing these state-
ments into the Query Tool, you can also run the code by clicking Open File
and navigating to the place where you saved the code you downloaded from
GitHub. Always do this if you see the code is truncated with --snip--. For
this chapter, you should open Chapter_03.sql and highlight each statement
before clicking the Execute/Refresh icon.

SELECT * FROM teachers;

Listing 3-1: Querying all rows and columns from the teachers table

Once you execute the query, the result set in the Query Tool’s output
pane contains all the rows and columns you inserted into the teachers table
in Chapter 2. The rows may not always appear in this order, but that’s okay.

id first_name last_name school hire_date salary


-- ---------- --------- ------------------- ---------- ------
1 Janet Smith F.D. Roosevelt HS 2011-10-30 36200
2 Lee Reynolds F.D. Roosevelt HS 1993-05-22 65000
3 Samuel Cole Myers Middle School 2005-08-01 43500
4 Samantha Bush Myers Middle School 2011-10-30 36200
5 Betty Diaz Myers Middle School 2005-08-30 43500
6 Kathleen Roush F.D. Roosevelt HS 2010-10-22 38500

2 Chapter 3

You might also like