Академический Документы
Профессиональный Документы
Культура Документы
SELECT
ORDER BY
WHERE
Logical Operators
JOIN
Aliases
After the SELECT you choose which columns you wish to display in your output
After the FROM you choose which tables these columns are in
By default all records are sorted in ascending order (A-Z). You can specify to sort in descending order as well (Z-A).
Although it is default, you can also explicity set the sort for ascending as well.
The following command sorts Column1 ascending (As it's the default) then Column2 as descending.
SELECT *
FROM TableName
ORDER BY Column1, Column2 DESC
The following command will do the same as above, but in this case we are explicity stating for Column1 to sort
ascending.
SELECT *
FROM TableName
ORDER BY Column1 ASC, Column2 DESC
LOGICAL OPERATORS
We can include logical operators in our WHERE clause.
These are two main logical operators
AND
OR
To understand this more, lets create a broken SQL Query and fix it using brackets.
SELECT City, Country
FROM Locations
WHERE Country = 'Canada' AND City = 'Vancouver' OR City = 'London'
ORDER BY Country, City
So what is the issue with this query? Well, both conditions to the left and right of the AND must be true. So only
results where the Country = 'Canada' and City = 'Vancouver' will be shown. Another issue, only one condition left or
right of an OR has to be true. So the results will show where the Country is Canada and the City is Vancouver but
also any city that is London regardless of the country.
How do we fix this?
The easiest way to fix this is to add in brackets.
SELECT City, Country
FROM Locations
WHERE Country = 'Canada' AND (City = 'Vancouver' OR City = 'London')
ORDER BY Country, City
So we added brackets around our OR statement values. Everything in the brackets to the right of the operator is
considered one value. So regardless if the city is London or Vancouver, they but also must be part of Canada.
There are other logical operators we can use with numerical values and dates. We already used '=' equil to, but here
are some more we can use.
JOINING TABLES
You can use the JOIN clause to join multiple tables together for your results.
Here is the syntax of a JOIN statement
SELECT *
FROM Table1
JOIN Table2
ON Table1.column = Table2.column
WHERE Column = valueORDER BY Column
For us to join tables, we need to know which columns reference each other. Refer to the Exhibit shown here, How
do we know which department Joe belongs to? Well, we know Joe belongs to the Sales department, but how do we
know this? We know this because we match up the ID_Dept column from the tblstaff table with the ID_Dept column
from the tblDept table.
If we wanted to show in our results our staff name and the department they belong to we need to join the tables
together. We also need to tell SQL Server how to match up which records belong to each other (The relationship).
Just like we did by matching up ID_Dept between the two tables.
Let us look at the join statement showing the staff name and their department
SELECT tblStaff.Name, tblDept.Name
FROM tblStaff
JOIN tblDept
ON tblStaff.ID_Dept = tblDept.ID_Dept
When defining which columns we want to see in the results, we specify the table name followed by the
column name. This is only necessary when we have an ambiguous column name. In this case Name will exist
twice in our results, once from the tblStaff and once from the tblDept table. SQL Server needs to know
which name you want to display.
Our first table to add into our results is defined after the FROM statement and is tblStaff
Our second table toa dd into our results is defined after the JOIN statement and is tblDept
Our relationship is defined after the ON statement. This is what we reference to understand which
department belongs to which staff member
Another note, column names in the relationship do not need to be named the same thing. Although they typically
are, you will find situations where they are not. The only thing to keep in mind is to make sure the correct columns
are referencing each other.
If in the example above we changed the ON to "ON tblStaff.ID_Staff = tblDept.ID_Dept" This would still display the
results, but they would be incorrect. In this case Bob would be displayed as being part of the IT Team but Bob is
actually part of the Marketing team. So make sure your relationships are correct.
INNER JOIN - This is the same as just using JOIN, this is the most common type of join.
LEFT OUTER JOIN (or LEFT JOIN)
RIGHT OUTER JOIN (or RIGHT JOIN)
FULL OUTER JOIN (or FULL JOIN)
This is our inner join statement, this will only return results where there is matching record on both tables.
In this case, you will see a result for Joe, George and Jane.
You will not see Bob, Ann or Marketing as there is no matching record from the other table. This is an inner join.
Next, let us make a left outer join statement.
SELECT tblStaff.Name, tblDept.Name
FROM tblStaff
LEFT JOIN tblDept
ON tblStaff.ID_Dept = tblDept.ID_Dept
This is our left outer join statement, this will return all results from the table on the left side of the join statement
regardless if there is a match on the right side. Records from the table specified on the right side of the Join will only
appear if they have a corresponding record from the table on the left.
In this case Joe, Bob, George, Jane and Ann's records will all appear. Bob and Ann's department will show up as Null
since they do not have a department. Marketing will not show up as there is no corresponding match within the left
table.
A right outer join is the opposite of this.
Finally, let us look at our full outer join.
SELECT tblStaff.Name, tblDept.Name
FROM tblStaff
FULL JOIN tblDept
ON tblStaff.ID_Dept = tblDept.ID_Dept
In this case all records from both tables are displayed regardless if there is a matching record from the other table.
In this case, Joe, Bob, George, Jane and Ann's records will all be displayed. Bob and Ann will have a Null department
value in the results as they do not belong to a department.
Also, Sales, IT and Marketing will also be displayed. Marketing will have a null value in the results as no staff are in
the marketing department.
ALIASES
If we join these two tables together and we want to show the name of the staff member and the name of the
department. The column headers would both say name.
In our case, we should assume someone could make the distinction between a person's name and the department's
name. But not all queries will be so simple.
If we had a table with customers name and we wanted to show their sales rep name. How would we know which
name was the sales rep name and who's was the customer's name. The table name the column comes from is not
shown in the result so we need to add an alias.
An alias allows us to rename the column shown in the result. This helps us show more intuitive column names in our
results.
To add an alias, all we need to do is append it to the column name in our select statement. There are two ways to
do this.
SELECT tblStaff.Name 'Staff Name', tblDept.Name 'Dept Name'
FROM tblStaff
JOIN tblDept
ON tblStaff.ID_Dept = tblDept.ID_Dept
In the results the column headers will show the alias instead of the column name. The alias is in the single quotes.
Another method to do the same thing is to use AS as shown here:
SELECT tblStaff.Name AS 'Staff Name', tblDept.Name AS 'Dept Name'
FROM tblStaff
JOIN tblDept
ON tblStaff.ID_Dept = tblDept.ID_Dept