Tappitytap.info

Select Statements

The basics

A Select statement simply retrieves data from the database. In this section we will look at the basics and breakdown how a statement is formed.

The simplest form that a Select statement is of the form:
SELECT * FROM table name
well actually that's not completely true: select 'abc' is, but its probably not that useful on a day to day basis.
So given that you would like to do something useful then selecting data from a database is what we are going to look at.

Both of these statements do basically the same thing: they both pull data from the customers table although the second statement only retrieves some of the columns from the table whereas the first retrieves all the columns.


	
SELECT * FROM Customers
	-- or --
SELECT CompanyName, City, Country, PostalCode, Phone FROM Customers
        

Sometimes we would like the names of the columns to be different than those in the table so we can change them for better names.


SELECT CompanyName, City as 'Location', Country, PostalCode AS 'Post Code', Phone FROM Customers
        

The 'AS' keyword creates an alias for the column. please note that the AS keyword is optional so in reality we don't actually need it, all you need to do is give the column a different name.

Also you may have noticed that some of the aliases have quotes ' arround them, this is needed if the new name has a space between the words that make up the name. sometimes the spaces can cause other issues so sometimes we use underscores eg. Post_Code

result set after running query

You can even alias the name of the table, and although not that useful at the moment it will become very useful later.


SELECT C.CompanyName, C.City as 'Location', C.Country, PostalCode AS 'Post Code', Phone FROM Customers AS C
    

Filters

The WHERE clause allows us to filter the information so that we can focus on a subset of data


SELECT CompanyName, City as 'Location', Country, PostalCode AS 'Post Code', Phone FROM Customers WHERE Country = 'Brazil'
	-- OR --
SELECT * FROM Customers WHERE Country Like 'A%'
	-- OR --
SELECT * FROM Customers WHERE LEN(Country) >= 8 
        

the expression can use many different types of expressions and can use functions built in to the system that you are using. so lets take a look at the LIKE statement. and the wildcard characters that we can use.

Wildcard character
description
%
Any string of 0 or more characters
_ (underscore)
one characters
[]
Any character in the range eg [abcdefg] or [a-g]
[^]
Any character in the range eg [abcdefg] or [a-g]


SELECT * FROM Customers WHERE Country Like 'B%' 
SELECT * FROM Customers WHERE Country Like '%B'
SELECT * FROM Customers WHERE Country Like '[ABC]%'
SELECT * FROM Customers WHERE Country Like '[^ABC]%'
SELECT * FROM Customers WHERE Country Like '_[^ABC]%'
SELECT * FROM Customers WHERE Country Like '[ABC]%' OR Country Like '[N-Z]%'
        

B% all countries whose first character is a B

%B all countries whose last character is a B

[ABC]% all countries whose first character is A B or C

[^ABC]% all countries whose first character is not A B or C

_[^ABC]% all countries whose second character is not A B or C

The last statement is all countries that start with A B or C, or have any character between N and Z inclusive at its first character


There are many different options that you can use of which these are only a few. As you use sql more you will come across many different options but the best source of information is in the documentation LIKE documentation