Sunday, 15 April 2018

Crash Course in SQL Part 2: SELECT


The SQL SELECT instruction is the most important single thing to master

It is the instruction that actually gives the answers to any questions

It can have up to six sections, as follows

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

But some of these are used a lot less often than others

SELECT   lists the things we want to see
FROM   specifies which places to get these from
WHERE   specifies which things we do or don't want to see
GROUP BY   allows for some grouping or totalling up
HAVING   allows us to specify some more things we do or don't want if we used GROUP BY
ORDER BY   specifies which order to list the results in



SELECT can be used on it's own without any of the other sections. There are relatively few sensible uses for this. One might be to see what the computer thinks the date and time is

SELECT Getdate() 

gives results:






Getdate() is a 'function'. More on these later



To be of much use, we have to SELECT from one or more tables.

The simplest use of SELECT is with the * (star) symbol which means 'Select Everything'

  SELECT *
  FROM [TESTDB].[dbo].[tblExampleTable]

gives results:













This is little caricature of real data which we can use for illustrating SQL. It is in a table called tblExampleTable. The [TESTDB].[dbo].bit gives more information on exactly where this table is


So the SELECT * lists all five records in full. The order it comes back in is the not controlled.

We may want to have the list sorted alphabetically. The ORDER BY section can be used for this

  SELECT *
  FROM [TESTDB].[dbo].[tblExampleTable]
  ORDER BY [Description]

gives













Or we might want it sorted by the (in this case meaningless) number data, with the highest first. So we would add DESC (short for Descending) to the ORDER BY bit as follows

  SELECT *
  FROM [TESTDB].[dbo].[tblExampleTable]
  ORDER BY [SomeNumber] DESC

which gives:












Often we will not want every field in the table shown in the results. So rather than SELECT * we can SELECT specific fieldnames

  SELECT [Description], [SomeOtherThing]
  FROM [TESTDB].[dbo].[tblExampleTable]













More often than not we will be interested in looking at a specific part of the data rather than seeing everything each time. We can use the WHERE section to limit which records are shown. For example

  SELECT [Description], [SomeOtherThing]
  FROM [TESTDB].[dbo].[tblExampleTable]
  WHERE [SomeOtherThing]='Green'

gives









or


  SELECT [Description], [SomeOtherThing]
  FROM [TESTDB].[dbo].[tblExampleTable]
  WHERE [SomeOtherThing]<>'Green'

gives









The <> means 'is not equal to' so is basically 'everything but'

It is not necessary for the data items used for selecting or listing to be displayed in the final results

That is the end of Part 2
In Part 3 we will look at counting








Saturday, 14 April 2018

Crash Course in SQL Part 1: What is SQL

Most people pronounce 'SQL' as 'sequel' although 'S Q L' is equally valid

It has different meanings:

One is 'Structured Query Language', which is the subject of this 'crash' course.

Another common one  is as a shorthand for Microsoft SQL Server, which is a set of database software products produced by Microsoft.

Microsoft SQL Server has its own version of Structured Query Language which it calls 'Transact SQL' or 'T-SQL' for short

Other software products have their own variations of Structured Query Language. For example Oracle provides 'PL/SQL'

Although there are slight differences between the different versions of SQL, the core is exactly the same. So if you learn SQL on one product you can adapt quickly to others.

Structured Query Language is a way of interacting with relational databases

Relational databases are those which can have many tables. In theory this will be designed in such as way as  to avoid duplication of data. And each table will have a unique key which allows it to be related to other tables

In real life the design and content of databases can get a bit more messy, for various reasons.

SQL is essentially an instruction, written out as text, which is 'thrown' at a database. The database will attempt to respond to the instruction

How the instructions are written and thrown - and where the results are displayed - will depend on what tools you have available, Typically there will be a utility (such as SQL Server Management Studio) which allows these things to be done in different panels visible together on the same computer screen

So we would write (or retrieve an existing) SQL script in one panel, hit an Execute button, and see the results displayed in another panel

.
The beauty of SQL is that the core is relatively simple

There are different types of instruction for different purposes

Computer people give these grand titles such as

  • Data Manipulation Language (DML)
  • Data Definition Language (DDL)
  • Data Control Language (DCL)


These are basically just groups of instructions for either doing things with data or setting things up


SQL Data Manipulation Language (DML) is mainly based around the following four types of instruction
  • SELECT
  • DELETE
  • INSERT
  • UPDATE
These instructions are used for looking at, getting rid of, collecting or changing data

Of these,  SELECT is the most often used as it is the one that actually gives the answers to any questions


SQL Data Definition Language (DDL) is mainly based around the following three types of instruction
  • CREATE 
  • ALTER
  • DROP

These instructions are the ones needed to set things up - and then changing them again later. We will not spend much time looking at these on this crash course

SQL Data Control Language (DCL)is mainly based around the following three types of instruction
  • GRANT 
  • REVOKE 
  • DENY

These are the instruction used to set out who can do what. Computer people are obsessed with stopping other people doing things, or looking into the innards of their creations. It's a shame as otherwise they are good people to have around. We will not spend any time looking at these 'permission' issues on this crash course. Just be ready to hit this nonsense when you try to use anything in the workplace

That is the end of Part 1.
In Part 2 we will look at SELECT  - the main big thing you will need to master

Here are a few links which may help flesh out the content of Part 1

https://www.w3schools.com/sql/sql_intro.asp

https://www.infoworld.com/article/3219795/sql/what-is-sql-structured-query-language-explained.html

https://bytescout.com/blog/what-is-sql-and-what-is-it-used-for.html

https://www.ntchosting.com/encyclopedia/databases/structured-query-language/

https://searchsqlserver.techtarget.com/definition/SQL







Crash Course in SQL Part 2: SELECT

The SQL SELECT instruction is the most important single thing to master It is the instruction that actually gives the answers to any que...