Learning by sample t-sql - part 5
-
Upload
soetam-rizky -
Category
Education
-
view
455 -
download
0
description
Transcript of Learning by sample t-sql - part 5
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Learning By Sample -
T-SQL : Part 5
Learning By Sample Series
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 1
Learning By Sample - T-SQL : Part 5
Foreword
Learning By Sample ?
So, why should I made this tutorial freely accessed by everyone ? Well, surely I am just nobody, I
also just another ordinary person with ordinary knowledge. However, I always feel that some persons in
the internet giving away their tutorial for free and I feel being helped by them. Then, I remember one of
my senior wisdom words : “If you want to be given more, then start to give more”. And of course, I really
believe those words, since that I already proof it to be right all the time.
Also, I’m not an English native speaker, however, I just try to improve my English in any other
way, especially in written format. Thus, I think writing is the best practice to improve my English freely.
However, you will find many grammatical errors in my tutorial, so please send me comments and also
suggestion to improve it.
Then, why the format using Learning By Sample series ? Is it really different with other tutorials
out there ? Ehm, I already wrote three books (in Indonesian) using this kind of format, and many readers
already contact me about how this format really help them to learn from the scratch. So, why in the
world I’m not re-create it in English format, right ? Eventhough actually, it is similar with Hands On Lab
series which already famous in Microsoft site previously.
All of my lesson also being designed as short samples and short time exercise. Thus, I hope that
each of tutorial series would take only at least 10-15 minutes maximum to learn. Why keep it short ?
Because many beginner (and even expert) will find boring whenever they must keep studying more than
15 minutes (but you will never get bored when you online in such time right ?).
Another reason is just because many of samples in this series come up from my lecturing task
exercise. So, I just try to compile all my lab exercises in order to keep it tidy and also reusable for my
students. That’s why you will find many unsorted course material inside these series, however, just take
a seat and enjoy the ride !
Requirements
So, what will you need before starting this simple tutorial ? First, you need a PC/laptop/netbook
which use at least Windows XP/Vista/Seven/Server as its operating system. And, you should also install
SQL Server (at least Express Edition) from version 2005 and above. And remember, if you have SQL
Server Express Edition, make sure that you already download and install its SQL Server Management
Studio too.
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 2
Learning By Sample - T-SQL : Part 5
Is that enough ? Of course not, you also need Northwind database attached to your database
server (which you can download it from download section of this website) that already widely being
used as sample database from SQL Server 2000. And if you ask, why use Northwind database instead of
Adventure Work database, the main reason is just because its simplicity. Northwind database contains
small number of tables and also it only covers small amount of records, thus beginner will feel
comfortable and fast in doing this lesson.
So, if you already download your Northwind script, then just open your SQL Server Management
Studio and then open the northwind.sql script file. Afterward, execute the script by pressing F5 key, and
that’s it, you will have a newly Northwind database attached to your SQL server.
Prior Knowledge needed
If you really want to follow this series, then you should aware that basic SQL knowledge really
needed. For example, this HOL wil not include any basic knowledge about T-SQL basic command and
also about the basic of SQL server.
And also, you should know the relationship diagram of Northwind database before you
understand this lesson. You can learn about Northwind relationship by creating Database Diagram in
SQL Server Management Studio. Please refer to previous lesson on how to create the diagram.
You also ought to know basic Select command (includes multiple table and aggregation) from
previous lesson (if you are really a beginner), however, if you feel good enough in SQL and merely trying
to improve your skill then just follow the exercises later. Have fun !
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 3
Learning By Sample - T-SQL : Part 5
HAVING
1. In previous lesson, you already try exercises for grouping and aggregating fields. But, what if the
condition in our query is containing aggregate function ? There’s no other way to solve it unless you
use Having keyword. The principal of this keyword is easy, it just the same with common condition,
but it must be started with keyword having and contain aggregate function. Enough for the theory,
let’s roll on…
a. Displaying category name which have sales omzet greater than 200 thousands.
Here is the result :
So, where’s the keyword go ? Focus on condition after group by clause here :
b. Displaying sales omzet percentage from all transaction which are greater than 150.000 and
sort it from the biggest one.
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 4
Learning By Sample - T-SQL : Part 5
Check the result :
Wow, we have double Cast in this query, this happen because we need to put % symbol
which only fit with varchar type :
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 5
Learning By Sample - T-SQL : Part 5
And check also the desc keyword that we use for sorting :
c. Want to try more complex sample ? Here we go, try to display whole product name which
their sales omzet goes below sales average in 1996
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 6
Learning By Sample - T-SQL : Part 5
Here is the result :
Now, pay attention to having keyword condition, it uses sub query for its comparison
operator.
So, the conclusion is that we must be creative in this query game.
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 7
Learning By Sample - T-SQL : Part 5
NULL
1. What is Null ? For sure, you can say that null is just empty value from a field. However, null value is
not equal with common empty value. For instance, if you fill a numeric field using zero, it’s not the
same with null. Confuse ? Okay then, let me show you the example :
a. Display company name and fax field of customer
Check the highlighted result which shows null value
b. Display employee’s bos
Here is the result :
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 8
Learning By Sample - T-SQL : Part 5
2. Now, let see how to create condition using this null value
a. Display customer which has no fax number
Let see the result here :
b. Display the highest level in employee
w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0
Page 9
Learning By Sample - T-SQL : Part 5
Then, who is the highest level ?
3. In next tutorial I’ll try to give you some lessons about batch query, but now I need to enjoy my black
kretek filter first after seeing Manchester United crunching Blackburn Rovers by seven stylish goals,
see ya…...