Professional Documents
Culture Documents
The way you retrieve data to get what our manager wants is to write a
query starting with the word SELECT. This chapter will show the core
elements for the most commonly used keywords we use to retrieve data
from the RDBMS known as SQL Server 2012.
READER NOTE: Please run the SqlGettingStartedSetup.sql script in
order to follow along with the examples in the first section of Chapter 3.
All scripts mentioned in this chapter may be found at
www.Joes2Pros.com.
31
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
study with the Joes 2 Pros SQL Queries series starting with Volume 1 for
beginners and work through to Volume 5.
Figure 3.1 This shows a query for all records and all fields from the ShoppingList table.
32
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
We are able to run this query to retrieve the information we want by either
clicking on the ! Execute button in the toolbar above the query window, or
by pressing the F5 button on our keyboard (some keyboard configurations
require pressing the fn (function) + F5 key simultaneously).
Figure 3.2 Use the drop down window and choose JProCo.
33
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
34
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
Figure 3.3 This shows a table with a space in the name will not run without using square
bracket delimiters.
Of course, as shown in the two earlier queries, we can put square brackets
around any table. In fact, code generated automatically by SQL Server
always creates these delimiters for every object in the database. The only
time we must use the square bracket delimiters is when table names are
separated by a space, have the same name as a SQL Server keyword (bad
idea), or are otherwise named in a way that is not obvious or easily
interpreted by SQL Server.
35
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
Figure 3.4 Delimiting a table whose name includes a space will allow the query to run.
36
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
(just like the keywords SELECT and FROM). We can solve many
conflicts between a table name and keyword(s) by using square bracket
delimiters. By placing these delimiters around the Grant table name, SQL
Server will know that this is not a keyword.
Figure 3.5 Delimiters instruct SQL Server that Grant is a table, not the keyword
GRANT.
Over time we will find that in reality, delimiters are only necessary on rare
occasions, as most database objects created use proper naming practices.
37
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
Exact Criteria
There is a big difference between selecting everything and being selective.
When questioning the business importance of database systems, we need
only to look at what a highly profitable search engine company does every
day. The world has billions of web sites and when we want a search
narrowed down to just a few sites of interest, the database a search engine
company uses will make the necessary calculations based on our unique
search query and make the selections best suited for our needs. This
information, presented as a plethora of links, entices us to click on them
and visit a specific web site, which drives revenue created by the search
engine company’s advertising business.
We need to switch back to the dbBasics context for this next example.
When we query the entire ShoppingList table, we have two items costing
$1.25. It is easy to spot two records out of seven with our own eyes.
However; finding two records from a million with our own eyes is like
looking for a needle in a haystack. Let’s tunnel through that haystack with
the WHERE clause. We want to limit our result set based on the Price
field being exactly $1.25. This is accomplished by adding this specific
criteria immediately after the WHERE clause (Figure 3.6).
SELECT *
From ShoppingList
WHERE Price = 1.25
38
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
The perfect tool for filtering data is the WHERE clause. There are still
seven records contained in the ShoppingList table, even though the
number of records in the result set of our query is now two.
Let’s switch back to the JProCo database context. The next query example
uses a WHERE clause to filter on the FirstName field looking for all
values that are equal to David. We have data represented by 12 records
stored in the Employee table. The WHERE clause will show only those
records that exactly match the criteria we give it and expects a logical
statement to evaluate each record. This logical statement (FirstName =
'David') is commonly known as a predicate. By predicating on the
FirstName field, our goal is to filter the results to provide us with only the
information that we want to see.
SELECT *
FROM Employee
WHERE FirstName = 'David'
39
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
Figure 3.7 This shows all the employees with a FirstName of David in JProCo.
Let’s try running the next few examples by ourselves, checking the results
after completing each query. Use the JProCo database context for these
examples.
Of the twelve employees, there are ten not named David. The reverse of
this query will show all records that do not have David as a FirstName:
SELECT *
FROM Employee
WHERE FirstName != 'David'
OR Criteria
Using the ‘=’ operator provides an exact match. What if we wanted to look
for all the values of Lisa or David in the FirstName field? One option is to
use this code:
SELECT *
FROM Employee
40
wwwJoes2Pros.com
Chapter 3 Writing Basic Queries
This works fine, except for the logical rule that only allows for one exact
match after the ‘=’ operator. Some people like using the equals sign and
simply write an additional line for each criterion that they are looking for.
What if the goal is to find everyone in the company except for Lisa and
David? By putting the word NOT in front of the IN operator we give the
opposite result. Since there are twelve employees and three named Lisa or
David, there will be nine employees not named Lisa or David. This code is
an example of how to use the NOT IN operator when specifying a list:
SELECT *
FROM Employee
WHERE FirstName NOT IN ('Lisa', 'David')
41
wwwJoes2Pros.com