Follow me on Twitter RSS FEED

Understanding SQL Joins - From beginners to advanced

Posted in By waelouf 5 comments

In this article I will show some many samples for SQL Joins, having the following simple database, this database contains all possible relations to can preview all types joins, we have 1-1, 1-many, many-many relationships (as in the figure).

 fig. (1)
As a start, we have 5 types of joins ("Inner" join, Left join, Right join, Full Join, Cross Join) and Self join will be represented as inner, left or right join as I will show in the next examples.

fig. (2)

Inner Join:


Let's start with Join (or Inner Join), as from the previous diagram, Inner join is the intersection between two tables, in our case I will use Books and Publishers tables, these tables have the following data:


fig.(3) fig. (4)
when we perform inner join, we will select from the table (Books) records where the "PublisherId" column is not null, so when we write the following Query:

SELECT  Books.BookName , 
        Publishers.PublisherName
FROM    dbo.Books
        JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID


 fig. (5)

As you notice, only records from table "Books" where "PublisherId" is not null - I'll talk about about the algorithm used in inner join later -, as in fig. (2), the inner join select only the intersection between two tables.

What Does the word Left and Right exactly means??

Before talking Left and right join, what does the words left and right actually represents in the query? if we take the previous query like in the following graph:

 fig. (6)



Left is the left input of the equality equation (in most cases the table that contains foreign key) and right is the input of the equality.

Left Join:

So when say I will use Left Join, this means you will select all records from the left table of the equality including the records in the intersection between the two tables, so if you looked to fig. (2), we will select the "Left" area UNION "Inner" area.

so if we execute the following query:

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        LEFT JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

We will have the following result:

 fig. (7)

Right Joins:

Also the same idea when you use right join, you select all records from the "right" area from fig. (2) union all records in the "inner" area, so if we executed the following query:

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        RIGHT JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

we will got the following result:

 fig. (8)

Fun with Left and Right joins:

After you understand what is left and right joins and what are the output of each one of them, the question now, what will be the result of of the of the next query?

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books 
   RIGHT JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID


SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Publishers 
 LEFT JOIN dbo.Books ON  dbo.Publishers.ID = dbo.Books.PublisherId

First notice what is the different between two queries? Left and right, and equality is vice-versa

fig. (9)

What about the result?? actually the result of these two queries are the same, since the first query I wrote Right join, and the table on the Right was Publishers, and the the second query I wrote Left query, and the table on the left was also Publisher, that's what I call magic :)

The Myth of the words "INNER" and "OUTER"

As you notice I did not use the keywords "inner" or "outer" in the last queries, what are they mean? and why many people uses them??

 fig. (10)

When you use these words, you just indicates to yourself what part I will select from this relationship, so when use "Inner" that means you will select the intersection between the two tables where where the foreign key in original table (Master Table) is not null, that means "Join" equals "Inner Join", so the next two queries are absolutely the same and will give the same results:

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        INNER JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

The same Idea for both left and right join, when you write left/right join, you actually select the outer part of the relationship (check both fig. (10) and fig. (2), left and right are outer records of the relationship), this means the following queries are the same:

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        LEFT JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID
and 

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        LEFT OUTER JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

The same for right join also

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        RIGHT JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID
and 

SELECT  Books.BookName ,
        Publishers.PublisherName
FROM    dbo.Books
        RIGHT OUTER JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

Full Join 

Full join is Union of both "Left" and "Right" Join, the full join get all data in tables in teh relationship, so in our sample, the Books table contains 13 records, the publishers table contains 7 records, the full join will get the 13 records of books union all records in table "Publisher" which are not in referenced in "Books" table, the syntax of "Full Join" is like the following query:

SELECT  BookName ,
        PublisherName
FROM    dbo.Books
        FULL JOIN dbo.Publishers ON dbo.Books.PublisherId = dbo.Publishers.ID

Wich will give the following value:

 fig. (11)

Cross Join:

Cross join used to get all available combination between two (or more) tables in the query, in our case the Books have 13 records, Publisher have 7 records, so all available records will be 13 x 9 = 91 records, and the syntax of cross join is a little bit different from any other join syntax, since you will not state the primary key and foreign key of the relationship "ON Table2.PrimaryKey = Table1.ForeignKey", so the query will be like this:

SELECT  BookName ,
        PublisherName
FROM    dbo.Books
        cross JOIN dbo.Publishers 

And will give the following result:

 fig. (12)

Self Join:

Self join do not have a special syntax, but it's accomplished by setting the same table name with an alias to can set a join syntax, and hence we can use either "Inner" or "Outer" (Left/Right) joins, but it will make more since if we used Left join because inner join will not give the accurate data, consider the following example, the "Books" table (Check fig. (2)) we have a column called "ParentBookId" which indicates if this book is a part of series of books the "ParentBookId" will be the Id of the previous book, so if we used the "INNER JOIN", with the following syntax:

SELECT  B1.BookName ,
        B2.BookName AS ParentBookName
FROM    dbo.Books B1
        JOIN dbo.Books B2 ON B1.ID = B2.ParentBookId

The result will be only 6 records (since INNER JOIN gets only where foreign key is not null), so the accurate result will be by using outer Join (Left or Right) like this:

SELECT  B1.BookName ,
        B2.BookName AS ParentBookName
FROM    dbo.Books B1
        LEFT JOIN dbo.Books B2 ON B1.ID = B2.ParentBookId

Which will get the following result:

 fig. (13)

How does Join actually works??(*)

1- INNER JOINS:

Considering the following query:

SELECT ColumnA, ColumnB, ColumnC, ...., FROM OUTER_TABLE 
       JOIN INNER_TABLE ON OUTER_TABLE.Column1 = INNER_TABLE.column2

When you execute this query the following algorithm applied:

for each row R1 in the outer_table
           for each row R2 in the inner_table
                    if R1 joins with R2
                          return (R1, R2)

In other way, the SQL Engine generates a nested loop, first loop takes every row from the OUTER_TABLE and in the other loop takes every row from the INNER_TABLE then check if the row from outer table joins with the row in the inner table, if not join continue loop, if joins return the two row.

2- OUTER JOINS

The same idea for Outer (Left) join:

SELECT ColumnA, ColumnB, ColumnC, ...., FROM OUTER_TABLE 
       LEFT JOIN INNER_TABLE ON OUTER_TABLE.Column1 = INNER_TABLE.column2

on executing this query, the following algorithm applied:

for each row R1 in the outer table
begin
    for each row R2 in the inner table
        if R1 joins with R2
            return (R1, R2)
        if R1 did not join
            return (R1, NULL)
end

In other way, the SQL Engine generates a nested loop, first loop takes every row from the OUTER_TABLE and in the other loop takes every row from the INNER_TABLE then check if the row from outer table joins with the row in the inner table, if two rows are joins, return two rows, else return outer row and NULL.

Other Joins Algorithms:

All previous example are done by using the default join algorithm (which is called Loop join algorithm), there are other types of joins, like:
  • Nested (Loop) 'Default'
  • Merge
  • Hash
I'll talk briefly about the other types.

Merge Joins:

Unlike the nested loops join which supports any join predicate, the merge join requires at least one equijoin predicate.  Moreover, the inputs to the merge join must be sorted on the join keys.  For example, if we have a join predicate “T1.a = T2.b,” table T1 must be sorted on T1.a and table T2 must be sorted on T2.b.

The merge join works by simultaneously reading and comparing the two sorted inputs one row at a time.  At each step, we compare the next row from each input.  If the rows are equal, we output a joined row and continue.  If the rows are not equal, we discard the lesser of the two inputs and continue.  Since the inputs are sorted, we know that we are discarding a row that is less than any of the remaining rows in either input and, thus, can never join.

We can express the algorithm in pseudo-code as:

get first row R1 from input 1
get first row R2 from input 2
while not at the end of either input
    begin
        if R1 joins with R2
            begin
                return (R1, R2)
                get next row R2 from input 2
            end
        else if R1 < R2
            get next row R1 from input 1
        else
            get next row R2 from input 2
    end

Unlike the nested loops join where the total cost may be proportional to the product of the number of rows in the input tables, with a merge join each table is read at most once and the total cost is proportional to the sum of the number of rows in the inputs.  Thus, merge join is often a better choice for larger inputs.

Hash Join:


it comes to physical join operators, hash join does the heavy lifting.  While nested loops join works well with relatively small data sets and merge join helps with moderately sized data sets, hash join excels at performing the largest joins.  Hash joins parallelize and scale better than any other join and are great at maximizing throughput in data warehouses.

Hash join shares many characteristics with merge join.  Like merge join, it requires at least one equijoin predicate, supports residual predicates, and supports all outer and semi-joins.  Unlike merge join, it does not require ordered input sets and, while it does support full outer join, it does require an equijoin predicate.

The hash join executes in two phases: build and probe.  During the build phase, it reads all rows from the first input (often called the left or build input), hashes the rows on the equijoin keys, and creates an in-memory hash table.  During the probe phase, it reads all rows from the second input (often called the right or probe input), hashes these rows on the same equijoin keys, and looks or probes for matching rows in the hash table.  Since hash functions can lead to collisions (two different key values that hash to the same value), we typically must check each potential match to ensure that it really joins.

for each row R1 in the build table
    begin
        calculate hash value on R1 join key(s)
        insert R1 into the appropriate hash bucket
    end
for each row R2 in the probe table
    begin
        calculate hash value on R2 join key(s)
        for each row R1 in the corresponding hash bucket
            if R1 joins with R2
                return (R1, R2)
    end


I'd like to finalize this post with a simple comparison between Nested Loop, Merge and Hash Joins:




(*) starting from this part, I used the material of session I attended couple of years earlier by (Mohamed Moshrif - Microsoft SQL Server Development Team), it was a brilliant session, and it was the main reason that I started loving SQL Server :)

Beyond Deadlines

Posted in By waelouf 0 comments

There has to be more to success than meeting deadlines... but what?

When I was a kid, I was happy just to play around. I loved the challenge of programming. When I got a program to work, it was a major victory. Back then, even a program that didn’t work was a success of some sort, as long as I had fun writing it. My definition of success centered on personal rewards.

As I gained experience, my software became more complicated and I often lost track of how it worked.
I had to abandon some programs before they were finished. I began to believe that maintainability was the key to success—an idea that was confirmed as I entered the workforce and began working with teams of other programmers. I prided myself on producing elegant, maintainable code. Success meant technical excellence.

Despite good code, some projects flopped. Even impeccably executed projects could elicit yawns from users. I came to realize that my project teams were part of a larger ecosystem involving dozens, hundreds, or even thousands of people. My projects needed to satisfy those people ... particularly the ones signing my paycheck. In fact, for the people funding the work, the value of the software had to exceed its cost. Success meant delivering value to the organization.

These definitions aren’t incompatible. All three types of success are important . Without personal success, you’ll have trouble motivating yourself and employees. Without technical success, your source code will eventually collapse under its own weight. Without organizational success, your team may find that they’re no longer wanted in the company.




The art of agile development
By James Shore, Shane Warden

Reports folder and Reporting Services !!

Posted in By waelouf 0 comments

I had a funny situation today and I had to tell you about it.

There was a server I need to deploy an application on it, and this application (ASP.NET 4 by the way) have a folder called (Reports) containing all reports of the application, any way the deploying process was so smooth and I finished deployment in only 15 minutes (a new world record :D)

After the deployment I started a fast tour to make sure the application is working correctly, and every thing was just great till I started the reporting module!

Each time I open a report I popup message appears to insert username and password (Windows Authentication) which is 100% wrong as the application uses (Forms Authentication), when I clicked Cancel I got an nice white page, next time I added the administrator's username and password to check what is the problem, I got the most beautiful error in the world ("404 The resource cannot be found."), but how!!!, the page exists, the report exists, the user is authorized to use this page, then I spent about 2 hours investigating this problem but in vain.

Next day, I started a new round with this error, the last attemp was about 15 minutes ago, I kept going up from the path of the report to the its folder, then parent folder till I reached the url "http://server's_IP/Reports" then I got great surprise, this server has SSRS (SQL Server Reporting Services) installed, configured and ready to use, and when I was trying to call my reports I was actually calling a report (witch does not exists) in the reporting services, thats why I got the error message 404, and he was absolutely correct, and when I renamed the folder from "Reports" to "Reporting" it worked!

Conclusion, do not call your reporting folder "Reports" especially if it was on the root of your application, because you do not know the environment of the client, even if you know it's very likely they start using reporting services and destroy your application's reporting module.

Good luck next time and I hope that was useful.

New on Google ... Change your background image !!

Posted in By waelouf 1 comments

When I checked Google today (June 5th, 2010) at 2:00 AM, I found a new link in the far bottom say (Change background image)


Then you can choose an image from a large image gallery




At the end you will find this outcome


Really nice work Google :)

Database Cleaner

Posted in By waelouf 0 comments


When a developer works on a long-term project, it could happen that he stop using some stored procedures for any reason (change request, fix bug, update a module, ... etc), and since the stored procedure is the most preferred way to perform all CRUD transactions on the database, so you can create at least 3 stored procedures per table, and it may cause a headache when you try perform a cleaning operation to remove unused objects specially the stored procedures.

"Clean your database" is a simple project removes unused stored procedures from your project what ever the Data Access Layer you are using

This application works and tested on the following DAL (Data Access Layers):
  • ADO.NET
  • Entity Framework
  • Linq 2 SQL
And it is pretty simple to use in only 3 steps:
  1. Browse the project's folder
  2. Set the connection string to the required database
  3. Start the operation

After the searching completed successfully, you can either export the cleaning script and apply the cleaning later, or by performing the cleaning immediately

Download the project source code and installer from CodePlex .

Arabic currency conversion from numbers to letters

Posted in By waelouf 0 comments

Original Post on CodeProject


Introduction

This project can be useful in many applications, like ERPs, financial applications, or any application used by Arabic speakers to display any number "as digits to characters", which is called - in some Arab countries - "tafqit" or "تفقيط".

Background

It's known that to write a number in Arabic characters, it's a little bit hard, as a number like 123 is written in English as "One Hundred twenty three", but in Arabic, it will be "One Hundred and three and twenty". So, for a big number - say 7 digits - it will be a little bit confusing to write it in this complicated form.

Using the code

The advantage of this code that it's pure SQL , so it will be easy to add it to any function, Stored Procedure, or View. Also, it can be used in both web and Windows applications. To use the code, first run the attached script in the desired database and then use the function called currency_conversion like this:

SELECT dbo.currency_conversion(12.10)


In the last example, the output will be:

currency
------------------------------------------
اثني عشر جنيها و عشره قرشا

(1 row(s) affected)


Points of interest

As I mentioned before, this function can be used in both Windows and web applications as it is just a SQL function. Also, as it's a scalar function, it can be used in both Views and Stored Procedures. You can also modify the script to fit your country's currency; just replace "Egyptian Pound" with your currency

Finally, to download the script, click HERE

The script contains the required table and functions, run it on any database (good or new) and then you can use the function mentioned before (SELECT dbo.currency_conversion(@number))

I hope this is useful.

Unforgivable Bug in Google Calendar Gadget !!!!!!

Posted in By waelouf 0 comments


This bug from Google Desktop, "Google Calendar Gadget V2.0", suddenly I discovered that April have 2 days 29th, and do not ask me how!!!!