Share via


The parts of a SQL query

I was going to go more into Joins this week, but as this is the Getting Started Blog, I think I should perhaps take a step back and go into some basic query stuff first. If you are not familiar with SQL – The Language, this is what we use to create queries to return data. This can be anything from very simple commands to return all the data from a table, or the queries can be complex enough that a PhD is needed to parse them. If I get to the latter state in this blog, I promise I will change the name of the blog! J

For the simpler queries, you will almost always start with the SELECT statement. This tells the query engine that you want to look at a table or tables, and take some or all of the data out of it. Next, you will either need to put in a star (*), or type in the column names you want to look at separated by a comma, such as:

SELECT column1, column2 FROM tablename WHERE condition = TRUE.

The FROM keyword is simple, that is the table you are taking the values from to get your result. The WHERE clause is where you filter your data according to various conditions, and anything after the WHERE clause is called the predicate. A predicate is an expression that makes a factual assertion about values, and it is therefore a Boolean expression and will evaluate to either True or False.

Language references

There are many places that you can find good references about SQL Queries, a good starting reference is not surprisingly the MSDN Library:

https://msdn.microsoft.com/en-us/library/ms691676(v=VS.85).aspx

In the Help contents of SSMS, you can also look under Database Engine/Technical Reference/Transact-SQL Reference, this is a good place to look up all the commands. This tree is found in the SQL Server Books Online section of the Contents.

Obviously this is an area where you can spend a heck of a lot of time and effort learning about queries, but the more you learn, the better you will be at using SQL Server (or any relational database for that matter), so it is not a bad idea to learn as much as you can.

Comments

  • Anonymous
    May 14, 2010
    ... now to learn "group by" and "having"? :-)