Wednesday, August 26, 2015

No-Table SELECT statements

A short video covering simple SELECT statements.


Very simple SQL SELECT statements without a table. No FROM clause!

For beginners. Covers renaming a column, selecting number and string literals, simple math expressions, string concatenation, and column ordering.



Here is the SQL that goes with the demo.

USE Northwind

-- basic no-table SQL statements

SELECT 54

-- look at the results
-- notice that this is a one-row, one-column table, with no name for the column

SELECT 'Hello'

-- similar to above, but with text characters
-- note that numbers are shown in black (if you are viewing in SQL Mgmt Studio)

SELECT '54' 

-- similar to first, but different, in that the result is NOT a number
-- note that character strings are shown in red

SELECT 54 AS [A Number]

-- here, we are assigning a name to the column
-- this is important, so that we can refer to the column if we want
-- note that "reserved words", are shown in blue

SELECT 2 + 2

-- here is a mathematical expression that is evaluated by the SQL engine on the remote server

SELECT 2 + 2 AS [The Number 4]


SELECT 2 * (17/3) AS [mathematical expression result]
SELECT 2.0 * (17.0/3.0) AS [mathematical expression result]

-- the math operators are similar to those in a spreadsheet
-- note that you can use parentheses to control the order of operations

SELECT 'Bar' + 'ney' AS [The name Barney]

-- here, we are using a character string expression
-- concatenating two character strings into a single character string

SELECT 'Barney' + ' ' + 'Rubble'  AS [A Flintstones character]

-- three strings concatenated to produce first and last names with a space between

SELECT  SQRT(34) AS [Square Root of 34]

-- you can use functions in an expression
-- the SQRT function calculates the square root of the number
-- functions show up as pink/magenta
-- functions have a name, then parentheses

SELECT   SQRT(34)                   AS [Square Root of 34],
         'Barney' + ' ' + 'Rubble'  AS [A Flintstones character]

-- you can generate more than one column in the table that is created by the SELECT
-- there's two columns defined here, with a comma between them

SELECT   SQRT(34)                   AS [Square Root of 34],
         'Barney' + ' ' + 'Rubble'  AS [A Flintstones character],
         2 * (17/3)                 AS [mathematical expression result]

-- here are three columns
-- note the comma at the end of each line

-- note also the formatting / spacing
-- you CAN write the above like this:

SELECT   SQRT(34) AS [Square Root of 34], 'Barney' + ' ' + 'Rubble'  AS [A Flintstones character], 2 * (17/3)  AS [mathematical expression result]

-- but it's harder to read

-- this is better

SELECT   SQRT(34) AS [Square Root of 34], 
'Barney' + ' ' + 'Rubble'  AS [A Flintstones character], 
2 * (17/3)  AS [mathematical expression result]

-- and even better

SELECT   SQRT(34) AS [Square Root of 34], 
         'Barney' + ' ' + 'Rubble'  AS [A Flintstones character], 
         2 * (17/3)  AS [mathematical expression result]

-- most readable is this

SELECT   SQRT(34)                   AS [Square Root of 34],
         'Barney' + ' ' + 'Rubble'  AS [A Flintstones character],
         2 * (17/3)                 AS [mathematical expression result]


No comments:

Post a Comment

Followers