SQL Training Online

  • Training Courses
  • Beginner Book
  • Blog

October 22, 2012 by Joey Blue Leave a Comment

SQL With – How to Use the WITH Statement/Common Table Expressions (CTE) in SQL Server – SQL Training Online

In this video, I introduce the SQL WITH statement (also known as Common Table Expressions or CTE) and show you the basics of how it is used.

[sharebox5_no_text] [/sharebox5_no_text]

The SQL WITH Statement is called Common Table Expressions or CTE for short in SQL Server

The SQL WITH statement is used for 2 primary reasons:

1) To move Subqueries to make the SQL easier to read.

2) To do recursive queries in SQL

Today, I just want to talk about the subquery piece.

I first want to take a look at the Employee table in the SQL Training Online Simple Database.

select * from employee

To talk about the SQL WITH statement, I have to first talk about and show you a subquery.SQL With Statement

select *
from
(
select * from employee
) a
So that is an example of a subquery.

But, we want to talk about the SQL WITH, which allows you to move the subquery up and make the SQL a lot easier to read.

Here is the same query using the WITH statement.

WITH cteEmployee (employee_number,employee_name,manager)
AS
(
select employee_number,employee_name,manager from employee
)
select *
from cteEmployee

You can see that we start with the WITH clause and then we can use any name we want to name our CTE. In this case, I use “cteEmployee”.

Then we need to specify the columns inside of parenthesis.

Next comes the AS clause.

And finally, we just SELECT from the cteEmployee table we created.

And, that’s it.

But, I want to take it a step further and join the cteEmployee CTE back to the Employee table and get the Manager’s name.

Here is an example of that.

WITH cteEmployee (employee_number,employee_name,manager)
AS
(
select employee_number,employee_name,manager from employee
)
select cte.employee_number
,cte.employee_name
,cte.manager
,e.employee_name as manager_name
from cteEmployee cte INNER JOIN employee e on cte.manager = e.employee_number

That’s it.

That is the basic introduction into the SQL WITH statement in SQL Server. Microsoft also has some good examples on this.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

 

Filed Under: SQL Training

October 19, 2012 by Joey Blue Leave a Comment

SQL Add – How to Use Addition in SQL Server – SQL Training Online

The SQL Add (+), or the Addition operator is used to add to numbers and days to a date.  In this video, you will see how to use it inside of SQL Server Management Studio.

[sharebox5_no_text] [/sharebox5_no_text]

I first want to take a look at the Employee table in the SQL Training Online Simple Database.

select * from employee 

In particular, I want to look at the Salary and Commission columns.SQL Add (+), SQL Addition

We can add those two columns together by using the plus sign (+), or addition operator in SQL:

select salary 
,commision ,salary + commision 
from employee 

But, since there can be nulls in the commission column, we need to handle that with the isnull function:

select salary 
,isnull(commision,0) as commision 
,salary + isnull(commision,0) as total_compensation 
from employee 

Next, I want to show you how to add with a datetime.

When you use the SQL Add operator, SQL Server will actually add the number in days.

Here is an example:

select hire_date 
,hire_date + 2 as training_date 
from employee 

This example will add 2 days to the hire_date.  MSDN also esplains the SQL Add operator.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

Filed Under: SQL Training

October 19, 2012 by Joey Blue Leave a Comment

How to use Datetime in SQL Server – SQL Training Online

This video is a quick overview of using the SQL Server Datetime function.

[sharebox5_no_text] [/sharebox5_no_text]

The datetime is actually made up of two different pieces:

1) Date

2) Time

This can be demonstrated using the SQL Cast function on a string to convert it to a datetime.

Here is some SQL for the Date portion:

select cast('20121019' as datetime)

Here is some SQL for the Time portion:

select cast('10:05:00' as datetime)

Now, you can also simply use the built-in getdate() function to bring back the current system time.

select getdate()

And finally, I want to bring back a datetime from an actual table. time

In this case, we will use the employee table from the SQL Training Online Simple DB:

select hire_date from employee

If we want to convert the result to a more formatted string, we will use the TSQL CONVERT function:

select convert(varchar,hire_date,104)
from employee

In this example, we are converting from a datetime to a varchar (string). The result will be in the format of 104.

To translate 104 to a datetime format, you will want to look at the MSDN Documentation.

And, that is how you use the Datetime SQL.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

Filed Under: SQL Training Tagged With: datetime, SQL

June 19, 2012 by Joey Blue Leave a Comment

SQL Count

The SQL Count is an important function that I use when I am exploring a new table.

I often use it to see how big a table is, but I also use it to determine counts within a group.

Let’s take a look at it’s uses.

Here is the employee table:

select * 
from employee; 

Results:

Select Results

You can tell this has 14 records, but imagine that it had 1,000,000 records.

You wouldn’t be able to just select * without it taking a while to bring all of the records back.

That is where the SQL Count comes in:

select count(*) 
from employee; 

SQL Count * Results

Here you can see it gave us one column with the count of the whole table.

Pretty simple.

Now, what if we wanted to find out how many people had a commission?

If we look above, we can simply count those people.  But, we have this new SQL Count function.

So, let’s use it:

select count(commision) 
from employee; 

Will we get 14 records, or 4 records?

Well, here are the results:

SQL Count Column Results

We only got 4 records.  Why is that?…

That is because the count function doesn’t count NULL records.  Again, if you look above, you will see a bunch of commissions that have the value of NULL.

This NULL value means we don’t have a value.  So, when the SQL Count  function looks at those rows, it ignores them.

But, look at the 10th row above.  It has a “0.00” value.  Why does it count that one?

Because, even though the value is 0, it is still a value (it isn’t null).

An that’s it.  That’s how you use the SQL Count function.

Filed Under: SQL Training Tagged With: Beginning SQL, SQL Function, SQL Select

June 18, 2012 by Joey Blue Leave a Comment

SQL Average

The SQL Average function is an easy function to use.

Let’s take a look at the Employee table:

select * from employee; 

Results:

SQL Average Results

If I want to find the average of the salary column, I would write the following query:

select avg(salary) as average_salary from employee; 

With the following results:

SQL Avg Results

Notice how we only get one column returned because that is all we asked for.

You can use the SQL Average on any numeric column using this technique.

When we get to the Group By clause, you will see how we can average across different groups of columns.

Filed Under: SQL Training Tagged With: Beginning SQL, Function, SQL Average, SQL Select

  • « Previous Page
  • 1
  • …
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • Next Page »

Recent Posts

  • SQL Database Normalization – 1NF, 2NF, 3NF, and 4NF
  • SQL Joins Tutorial for Beginners – Inner Join, Left Join, Right Join, Full Outer Join – SQL Training Online
  • Zillow House Price Analysis from CSV Archive – #PowerBI 002
  • Learn Basic SQL – 1 Hour Training Course – SQL Training Online
  • Create Table Statement in SQL Server and Inserting Baseball Homerun Leader Dataset – SQL Training Online

Popular Posts

  • SQL Functions
  • SQL Jobs in Oracle and Microsoft SQL Server
  • Troubleshooting a SQL Join
Copyright © 2025 · SQLTrainingOnline.com · Consulting at EmbarkBlue.com