-- Database by Doug
-- Douglas Kline
-- 8/9/2018
-- FROM - setting the source of data
USE Northwind
-- might want to review the SELECT statement
/*************************************/
-- we can do SELECT statements like this to create a tabular result
SELECT 'Doug' AS [firstName],
22 AS [age]
-- the data in this table is not stored anywhere
-- it was created in the SELECT statement
-- and placed in the tabular result
-- in the way we specified
-- but there's data in the database already
-- it's in a table
-- how do we get at it and display it?
-- we can do this
SELECT ProductID,
ProductName
FROM Products -- the important part!
-- the FROM statement sets the source of the data
-- Products is the name of a table
-- in the Northwind database
-- (note the USE statement at the top of this code)
-- I also happen to know that there are fields
-- called ProductID and ProductName
-- in the Products table
-- notice what happens when I try to use
-- a column that doesn't exist in the table
SELECT ProductID,
ProductName,
NonsenseField
FROM Products
-- intellisense underlines it in red
-- and when I run it, I get an "invalid column name" error
-- what do I do if I don't know what the columns are?
SELECT *
FROM Products
-- so what does the asterisk mean?
-- normally you put column names in the SELECT clause
-- when you put a * instead, it means "all columns"
-- **Important: SELECT * means ALL COLUMNS
-- ** it doesn't say anything about rows
-- ** which rows is defined by the WHERE clause
-- ** see the WHERE clause video
---------
-- so, let's type a new SELECT statement
/*
SELECT ProductID,
ProductName
FROM Products
*/
-- writing SELECT first:
-------------------------------
SELECT ProductID,
ProductName
FROM Products
---------------------------------
-- notice how the intellisense isn't very helpful
-- now writing FROM first:
-----------------------------------
SELECT ProductID,
ProductName
FROM Products
-----------------------------------
-- notice how intellisense is helpful
-- once I put the FROM statement in
-- remember how you can rename a column in the SELECT?
SELECT ProductID AS [ID],
ProductName AS [Name]
FROM Products
-- you can also do this for the table names
SELECT ProductID AS [ID],
ProductName AS [Name]
FROM Products AS [p] -- important part
-- this is called an alias
-- p is an alias for Products
-- you can use the AS statement
-- but the convention among professionals is normally to leave it out
-- like this:
SELECT ProductID AS [ID],
ProductName AS [Name]
FROM Products [p] -- Note no AS
-- note also that the square brackets are not necessary
-- you can do this:
SELECT ProductID AS ID,
ProductName AS Name
FROM Products p
-- I like to use them to highlight the aliasing
-- and point out that [p] is a name that was made up
-- in the SELECT statement - it doesn't exist in the table
-- there are times that you MUST alias a table or column name
-- to achieve your objective
-- (for example: same table in the FROM multiple times - more advanced)
-- it also has implications to "qualifying" a column name
-- here is the same query fully qualified
SELECT Products.ProductID,
Products.ProductName
FROM Products
-- the *not* fully qualified works
SELECT ProductID,
ProductName,
Products.SupplierID
FROM Products, Suppliers
-- but relies on the db engine
-- to look in the Products table for the column names
-- if there are no ambiguities, everything is fine...
-- if the column names are not reserved words, everything is fine...
-- when you have multiple tables in the FROM (see JOIN video),
-- you'll NEED to fully qualify to resolve ambiguities
-- AND to write safe, high quality code
-- sometimes you'll see this:
SELECT [p].ProductID,
[p].ProductName
FROM Products [p]
-- which works fine
-- the products table is aliased to p
-- which forces me to use [p] to qualify the fields
-- this doesn't work:
SELECT Products.ProductID,
Products.ProductName
FROM Products [p]
-- once it is renamed, it doesn't exist anymore
-- in the context of this statement
-- also, note that many would write the above statement
-- without the square brackets:
SELECT p.ProductID,
p.ProductName
FROM Products p
-- this works fine
-- and it will sometimes save quite a bit of typing
-- however, it doesn't scale to many tables well
-- it becomes *very* difficult to understand
-- as your statements get bigger with more tables,
-- I think this is clearer
SELECT Products.ProductID,
Products.ProductName
FROM Products
-- for me, code clarity trumps almost everything
-- I will type more so my code is very readable to others
-- (and myself, a month later!)
-- therefore,
-- I prefer to *avoid* aliasing tables
-- unless it is really necessary:
-- for example, when there are multiple of the same table in a single
-- in summary,
-- FROM is to state the source table(s)
-- you can alias table names in the FROM
-- you can, and sometimes HAVE TO,
-- *fully qualify* your column names
-- by using this format:
-- tableName.columnName
-- thanks for watching!
-- Database by Doug
-- Douglas Kline
-- 8/9/2018
-- FROM - setting the source of data
Tuesday, August 21, 2018
The FROM clause in SQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment