Tappitytap.info

SQL

Getting started with SQL

Accessing a database using SQL is a very common practice. What is also common is searching the internet in the forlorn hope of finding an example.

SQL is somewhat different than other languages generally individual statements can be quite complicated it makes no difference on whether you write the statement on one line or several. Although you can layout a statement so that it looks like several lines of code but at the end of the day it is still processed as one statement.


    -- All on the same line --
SELECT * FROM Customers WHERE CustomerID = 'ALFKI'

-- each part on different lines --

SELECT
*
FROM
Customers
WHERE
CustomerID = 'ALFKI'
        

The results will be the same.
The only reason for putting the parts of a query on different lines is just so that you can read them more easily.


SELECT * 
FROM Customers 
WHERE CustomerID = 'ALFKI'
        

And now the part that no one seems to have the time to do when they first start...

The most important part about learning SQL is not about SQL its about the structure of the documentation. Yes you heard right - you need to read the manual.

Before you panic or cry in despair I don't mean read all the sql documentation, but there is one part that is an absolute must. It describes how to read the documentation.

Select statement syntax

SELECT [ ALL | DISTINCT ] [ TOP ( expression ) [ PERCENT ] [ WITH TIES ] ] <select_list> <select_list> ::= { * | { table_name | view_name | table_alias }.* | { [ { table_name | view_name | table_alias }. ] { column_name | $IDENTITY | $ROWGUID } | udt_column_name [ { . | :: } { { property_name | field_name } | method_name ( argument [ ,...n] ) } ] | expression [ [ AS ] column_alias ] } | column_alias = expression } [ ,...n ]
SELECT Syntax

I'm sure that you have seen this at some time or at least you will do. most people ignore the help that it is offering, but please don't it will probably be the most effective 5 minutes that you will ever have whilst learning this language.

So here we go...

Transact-SQL Syntax Conventions MySQL syntax documentation Oracle Graphic Syntax Diagrams
Convention Used for
UPPERCASE Transact-SQL keywords.
italic User-supplied parameters of Transact-SQL syntax.
bold Type database names, table names, column names, index names, stored procedures, utilities, data type names, and text exactly as shown.
underline Indicates the default value applied when the clause that contains the underlined value is omitted from the statement.
| (vertical bar) Separates syntax items enclosed in brackets or braces. You can use only one of the items.
[ ] (brackets) Optional syntax items. Don't type the brackets.
{ } (braces) Required syntax items. Don't type the braces.
[,...n] Indicates the preceding item can be repeated n number of times. The occurrences are separated by commas.
[...n] Indicates the preceding item can be repeated n number of times. The occurrences are separated by blanks.
; Transact-SQL statement terminator. Although the semicolon isn't required for most statements in this version of SQL Server, it will be required in a future version.
< label> ::= The name for a block of syntax. Use this convention to group and label sections of lengthy syntax or a unit of syntax that you can use in more than one location within a statement. Each location in which the block of syntax could be used is indicated with the label enclosed in
chevrons: A set is a collection of expressions, for example ; and a list is a collection of sets, for example <composite element list>.

SELECT CompanyName, ContactName, Country
FROM Customers
WHERE CompanyName LIKE 'A%'
ORDER BY Country DESC
    

Now don’t be tempted to skip this, all you need to know is right at hand.

Looking at the statement above a sample select statement.

SELECT So SELECT is a keyword

everything in [] is optional

FROM : FROM is a keyword but its optional

WHERE is optional so I can choose if I want to filter the data

The | means 'OR' so I could pick one [ASC | DESC] in this case [] means its optional to pick a sort order but if i do then is should use ASC or DESC

All the other parts of the syntax are in the documentation relating to the individual statement so are also immediately accessible

So the above example should now make much more sense, by following the 'TSQL syntax conventions' documentation there is no need to search the internet any more looking for an example.