If you haven’t used a sub query yet, I’m surprised. They can be incredibly useful. They can also be a crutch…but like I say time and again, each tool has it’s use. I’d like to go into a little detail on sub queries today by talking about correlated sub queries. Let’s say you wanted to…
Tag: SQL201
Correlated Sub Queries with Aliases
I know I’ve covered sub queries before, but I wanted to make sure I made a specific point. When you create a correlated sub query, you can reference columns from your subquery anywhere in your outer query. Usually you’ll either use it in your SELECT clause or your WHERE clause, but you can use it…
COUNT(*) vs COUNT(columnName)
Let’s look at the following code using your copy of AdventureWorks. USE AdventureWorks GO SELECT COUNT(*) AS countStar , COUNT(ALL EmployeeID) AS CountAllEmployeeID , COUNT(DISTINCT EmployeeID) as CountDistinctEmployeeID FROM HumanResources.JobCandidate You should get three counts countStar CountAllEmployeeID CountDistinctEmployeeID 13 2 2 You need to understand the differences in these counts. COUNT(*) returns the number of…
Correlated Subqueries
The fundamental idea behind sub queries is you execute the sub query once, then take the value(s) from that sub query and substitute them in place of the sub query for the outer query. That’s quite a mouthful. Just think of it like this, the database engine will run the inner query, then replace the…
Error Number
Every error generated in Microsoft SQL has a specific number, this number should uniquely identify the error that has occurred. This can be useful in building logic on what to do when a certain error is raised since creating a logic test for a number is far quicker than generating one for a string. Let’s…
sql server 2000 + How to display date time
Over on Experts Exchange I saw a question that I get all the time, “How do I format this DATETIME like x?” If they’re asking to learn, I point them over to the CAST and CONVERT article on msdn. If you wish to change a Microsoft SQL DATETIME column from the standard format 2008-01-28 12:43:13.210…
SQL 201 – Contstraints
Constraints… helpful in making sure you get the data you want out of your database! Constraints allow you to define rules that the data must follow in order to be inserted into a record. You might want to define a primary key or foreign key in order to define the relationship of two related records…
Exception Handling
Learning to handle errors better can be one of the skills that will really set you apart from other SQL Developers. You can do constant unit testing after each change, but odds are you’ll never hit ever scenario your code will face. There is a better way. If you have programmed in any language like…
Identity Columns
An IDENTITY column is a column that automatically gets it’s value set by the database engine when a new record is added. This is one of the oldest ways Microsoft SQL has of making sure a record is unique. Even if a user were to insert the same record twice, the IDENTITY will always be…
Computed Columns
What is a Computed Column A computed column is a column that users will not enter data for, but the SQL Server will compute and store a value in. The expression for a computed column may include the names of other columns in the table. These columns can also be combined with literal values and…