Learning by sample t-sql - part 3

12
www.BukanSembarang.Info©2010 Learning By Sample - T-SQL : Part 3 Learning By Sample Series

description

For more tutorial visit www.bukansembarang.info

Transcript of Learning by sample t-sql - part 3

Page 1: Learning by sample   t-sql - part 3

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 3

Learning By Sample Series

Page 2: Learning by sample   t-sql - part 3

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 3

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.

Page 3: Learning by sample   t-sql - part 3

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 3

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 !

Page 4: Learning by sample   t-sql - part 3

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 3

Sub Query

1. Sub query concept is easy, just assume that you have another query which being placed in other

query. But, why should we have sub query ? Take a deep breath and come on try these samples :

a. Display product which never been sold in 1998

• Check the product which have been sold in 1998 at first

• Then, combine it with the master table which is product

• Take a closer look to not in keyword which become the main actor in this query

Page 5: Learning by sample   t-sql - part 3

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 3

• Here is the result :

b. Want another sample ? Let’s show customer’s name which never have sales in 1997

• Just like previous sample, display the opposite condition : customer’s name which

have sales transaction in 1997

• Then use not in keyword to display the reverse condition from its master table

• Check the result :

Page 6: Learning by sample   t-sql - part 3

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 3

c. Okay, next try this sample which use two condition. Present product’s category which

already have been bought by customer from Spain in 1997

Here is the result :

d. Displaying sales percentage for each customer in 1998, this kind of display commonly

needed in charting process.

Page 7: Learning by sample   t-sql - part 3

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 3

• First, check the whole omzet sales summary in 1998. This will be the sub query

which will be placed as arithmetic operation, not as condition.

• Then summarize each product’s category sales omzet

Page 8: Learning by sample   t-sql - part 3

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 3

• Next, let’s combine both query.

• Want to take a look of its result ?

Page 9: Learning by sample   t-sql - part 3

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 3

• Just pay attention to the division operation between sum for each category and

whole sales omzet which come from sub query.

• It’s easy right ?

e. Okay, let’s try another exercise. Try to display employee name which never sold anything to

Portugal

Page 10: Learning by sample   t-sql - part 3

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 3

Here is the result :

So, where’s the sub query go ? Let me show you…

f. Well, sound easy right ? Then try this last one. Displaying sales omzet for all product which

have been sold to Norway in percentage format.

Page 11: Learning by sample   t-sql - part 3

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 10

Learning By Sample - T-SQL : Part 3

Check the result :

Page 12: Learning by sample   t-sql - part 3

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 11

Learning By Sample - T-SQL : Part 3

2. Well, I think that’s enough for a moment and it’s time for me to take a short nap in this beautiful

Sunday , see you in next tutorial…..