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

SQL SELECT Statement Basics

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)
2 views1 page

SQL SELECT Statement Basics

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

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

Common questions

Powered by AI

The document suggests using the GitHub repository where the full SQL code is available, especially if code shown in the text is truncated. By opening the complete code in the Query Tool, users bypass issues caused by incomplete transcription. This approach supports effective learning by providing access to accurate, whole examples which aid in understanding SQL execution and structure. It allows learners to focus on the behavior and result of statements rather than troubleshooting syntax errors .

To execute a basic SQL SELECT statement in PostgreSQL using pgAdmin, you start by opening pgAdmin and selecting the appropriate database. Open the Query Tool, where you can write your SQL statements. For example, to retrieve all data from a table, you'll write 'SELECT * FROM teachers;'. The asterisk represents all columns, 'FROM' specifies the table, and the semicolon marks the end of the statement. Then, execute the statement using the Execute/Refresh icon. These steps ensure the query runs and the result set is displayed in the output pane .

The SELECT statement to retrieve only the 'salary' of teachers hired after 2010 would be: 'SELECT salary FROM teachers WHERE hire_date > '2010-01-01';'. This approach is beneficial as it limits the data retrieval to only relevant columns and records, thereby reducing data processing load and focusing the query on specific business logic. Such specificity helps in efficient resource utilization and clearer result sets .

A basic SQL SELECT statement comprises several key components. The 'SELECT' keyword initiates the query, followed by columns to retrieve or an asterisk for all columns. The 'FROM' keyword specifies the table name, indicating the source of data. Finally, a semicolon ';' ends the statement, signaling to the database that the query is complete and ready for execution. These components together form the backbone of any SQL query, facilitating data retrieval from database tables .

The GitHub code provided in the document serves as a practical resource for executing SQL queries discussed in the text. By navigating to the downloaded code and opening it in the Query Tool, users can execute ready-made SQL statements, which is especially useful when code in the text is truncated. This enhances learning by allowing hands-on practice and ensures the execution of functional, error-free queries. It’s a convenient aid for users to explore and understand SQL syntax and operations without typing errors .

Executing 'SELECT first_name, last_name FROM teachers WHERE school = 'Myers Middle School';' would return rows where the 'school' column matches 'Myers Middle School', showing only the 'first_name' and 'last_name' columns. This demonstrates how SELECT statements can be tailored using WHERE clauses to filter datasets based on specific conditions, which is useful for extracting targeted data subsets from larger datasets .

In SQL queries, rows may not appear in a specific order because SQL databases do not guarantee implicit sorting of results without an ORDER BY clause. The lack of ordering might be due to the storage implementation and query processing by the database engine. To ensure results appear in a specific order, you can use the ORDER BY clause to explicitly define the sorting criteria based on one or more columns, such as 'ORDER BY hire_date DESC' to sort by hire date in descending order .

In a SQL SELECT statement, the asterisk (*) wildcard serves as a shorthand to select all columns from the specified table. Its usage simplifies queries when all data from a table is required without specifying each column explicitly. However, while convenient, using the asterisk may have performance implications, especially in large tables, as it retrieves potentially unwanted columns and increases data retrieval load. It is often more efficient to specify only the necessary columns, thereby reducing overhead and enhancing query performance .

Explicitly specifying columns in a SQL SELECT statement, rather than using the wildcard, improves query performance and readability. By selecting only the necessary columns, you limit the amount of data retrieved, which optimizes data transfer and processing time, especially in large datasets. It also adds clarity to the query by making it explicit what data is being retrieved and reduces potential security risks of exposing unnecessary data .

The SELECT statement can be used for simple or complex data retrieval tasks. A simple application involves using the statement to fetch all rows and columns from a table using 'SELECT *', as demonstrated with the 'teachers' table example. In contrast, complex queries involve linking multiple tables through JOIN operations, applying conditional filters with WHERE clauses, performing aggregate calculations, and using advanced functions and subqueries to manipulate and condense large datasets into meaningful insights .

You might also like