Feeds:
Posts
Comments

It seems that we all love to solve puzzles. On SQLAuthority Page, we have been playing the number game and those who are playing with us know how much fun we are having. Sometimes, the answers are so innovative and informative that they open up those aspects of the technology which I have not thought of.

Today, I have a very relaxing puzzle for all of you. It is based on my earlier blog post on INNER JOIN and NULL, so I suggest reading the said post first if you want to get the complete idea.

The puzzle has three parts:

  1. Setup
  2. Fill in the blanks – T-SQL code
  3. Query and Resultset

We will set up the sample table first. We will run the sample query, which will give us some result. You will have to figure out what the missing script will be, which in turn will generate a specific displayed result.

USE TempDB
GO
-- Sample Table
CREATE TABLE FirstTable (Col1 INT)
CREATE TABLE SecondTable (Col1 INT)
GO
ALTER TABLE FirstTable ADD CONSTRAINT
UX_FirstTable UNIQUE NONCLUSTERED (Col1);
ALTER TABLE SecondTable ADD CONSTRAINT
UX_SecondTable UNIQUE NONCLUSTERED (Col1);
GO
INSERT INTO FirstTable (Col1)
VALUES (NULL)
GO
INSERT INTO SecondTable (Col1)
VALUES (NULL)
GO
--------------------------------------
-- Missing T-SQL Statement
--------------------------------------
-- SQL Query
SELECT st.col1
FROM FirstTable ft
LEFT JOIN SecondTable st ON st.Col1 = ft.Col1
GO
-- Clean up
DROP TABLE FirstTable;
DROP TABLE SecondTable;
GO

Current Resultset:

Expected Resultset:

Try harder. You will figure it out. If you cannot figure out and you want to give up, here is the answer.

If you do not feel like doing SQL any further, try the following image of Mars taken by Viking 1 in 1976.

Try to find the human face:

Do let me know if you like this kind of puzzle and we will have many more of  these.

Reference: Pinal Dave (http://blog.sqlauthority.com)

I blog and engage with the community because it gives me satisfaction when someone resolves an issue. A few days ago, I blogged about a DBA who began his first day at a new company and could not find out where the installation summary file was. He was very happy when I featured his story on our blog. Today he asked me another question and when I received his question my first reaction was – not possible. Later I said, may be possible, and when he shared more information, I said of course it is possible and natural. Let us go over these conversations in the exact order of how they occurred.

(I have modified the emails to chat format as it is easier to understand)

Jeff: Pinal, help needed. My query is returning more data than I need.
Pinal: Well, check your WHERE clause, JOINS etc.
Jeff: The problem is that INNER JOIN is returning more records than exist in the table.
Pinal: Not possible. How can a query return data that does not exist. Are you sure that you are connected to the correct database and running the query on the same server where you are validating the data?
Jeff: Yes, yes, everything is in the right place. Can Inner Join ever return more data than exists in the table?
Pinal: Well, maybe. Here is the story. Inner Join can for sure return more records than exists in the table, but it cannot return records that do not exist. Does that make sense?
Jeff: No.
Pinal: Okay, in simple words, if your table has three rows (values 1, 2, 3), your inner join can return 10 rows but it cannot return you the value 4 as part of the result.
Jeff: In simple English please…
Pinal: Honestly, I cannot make it more simple than what I said just now. Here is the example for you.
(Jeff goes through example and after few minutes)
Jeff: Exactly, I had no equal condition which did this. You helped me again.
Pinal: You’re welcome!
(After few minutes…)
Jeff: Pinal, help needed…

Well, this goes on. Anyway, here is the script that I sent him.

Let us create a scenario. We will create three sample tables and insert values in it.

USE TempDB
GO
-- Sample Table
CREATE TABLE FirstTable (Col1 INT)
CREATE TABLE SecondTable (Col1 INT)
CREATE TABLE ThirdTable (Col1 INT)
GO
INSERT INTO FirstTable (Col1)
VALUES (1), (2), (3), (NULL)
GO
INSERT INTO SecondTable (Col1)
VALUES (1), (2), (3), (NULL)
GO
INSERT INTO ThirdTable (Col1)
VALUES (2), (2), (2), (2), (2), (NULL)
GO

Here you will see the normal use of INNER JOIN where a query returns the results as the number of rows or fewer than the number of rows in the table. This happens because the data is unique in both of the tables.

Query 1: Inner Join with Equal to Sign

-- Query 1
SELECT f.Col1 fCol1
FROM FirstTable f
INNER JOIN SecondTable s ON s.Col1 = f.col1
GO

Query 2: Inner Join with Not Equal to Sign

Here is a query where the data is still unique (distinct) among tables but the result set contains more records than the numbers of rows the table contains. The condition <> (not equal to) returns the result which was excluded in the query where = (equal to) condition was used.

-- Query 2
SELECT f.Col1 fCol1
FROM FirstTable f
INNER JOIN SecondTable s ON s.Col1 <> f.col1
GO

Note: If you combine the data of Query 1 and Query 2, you will get the same result as CROSS JOIN of both the tables. If you want to understand further, read my earlier article Introduction to Join – Visual Explanation.

Query 3: Inner Join with Non-Distinct Row Table

Here is a query where data in another table is not unique and INNER JOIN returns more records than one of the tables.

-- Query 3
SELECT f.Col1 fCol1
FROM FirstTable f
INNER JOIN ThirdTable s ON s.Col1 = f.col1
GO

Summary

Inner Join can for sure return more records than the records of the table. Inner join returns the results based on the condition specified in the JOIN condition. If there are more rows that satisfy the condition (as seen in query 2), it will return you more results. Reference: Introduction to Join – Visual Explanation.

Well, feel free to send your questions or even solutions. If I find them interesting I will share them with everybody with due credit.

Reference: Pinal Dave (http://blog.sqlauthority.com)

SQL Server is an ocean of information. I believe if one starts learning today, after 60 years he/she may still be learning the subject (there are always a few exceptions)! Recently, I published the SQL Server Questions and Answers video tutorial, and since the course came out, I have been receiving lots of request to share SQL Tips which are small and easy to digest.

While writing the SQL books with my co-authors Vinod Kumar and Rick Morelan, we often came across very interesting and useful tips which we believe would be helpful to readers. Sometimes the tips are so small that we could not find an appropriate place for them in the book. We call them Sixty Seconds Tips. The tips are so small that sometimes sixty seconds are too long for them. Keeping the notion of quick and easy tips – we have decided to come up with new series called “SQL in Sixty Seconds.” Every week we will post one small but useful video tip. We believe that Sixty Seconds is metaphor for quick and easy tip.

I have seen many developers staying away from CTE (Common Table Expression), their excuse is that they do not know how to convert subqueries to CTE or they think it is very complicated to do so. In the very first episode of SQL in Sixty Seconds we will see how easily one can convert Subqueries to CTE and CTE to Subqueries.

More on CTE:
Simple Example of Recursive CTE
Multiple CTE in One SELECT Statement Query
Common Table Expression (CTE) and Few Observation
Delete Duplicate Rows
Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop
T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table

I encourage you to submit your ideas for SQL in Sixty Seconds. We will try to accommodate as many as we can.

Reference: Pinal Dave (http://blog.sqlauthority.com)

Here is email received from user:

“Pinal,

I am new DBA in my organization and I have to manage SQL Server 2005, 2008 and 2008 R2. Today is my first day at job and my manager has asked me to install all these different edition on our test environment. I have finished installing them. Later he has asked me provide him Installation Log Summary. I searched on internet and I could not find it, would you send me format of the installation log summary?”

I like this question, even though it is very simple, it demonstrates how new job can be confusing on very first day.

I sent him following reply

“Jeff,

I believe your manager is asking you following summary log produced by SQL Server installation. You can find it from following location on various version

Let us assume that your installation directory is: C:\Program Files\Microsoft SQL Server

SQL Server 2012 - C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\Log\Summary.txt
SQL Server 2008\R2 - C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt

Congratulations on first day!”

He immediately replied that I was correct, his manager was looking for summary file in above mentioned folder. You should replace the path of your installation directory with the C drive I specified in above response.

This remind me my first day at job. I was very much confused and lost too. I did not know the abbreviation and jargon used in organization. Thanks to my seniors and co-workers that I am now familiar with the same. However, the learning is forever ongoing activity. Every single day I learn something new when I walk into my office, I consider every day as my first day for learning something new.

Reference: Pinal Dave (http://blog.sqlauthority.com)

Recently one of my friend sent me email that he is having some problem with his very small database. We talked for few minutes and we agreed that to further investigation I will need access to the whole database. As the database was very big he dropped it in common location (you can use livemesh or dropbox  or any other similar product) and I picked up from the location.

I was able to install the database successful. He informed me that he has created database diagram so I can easily understand his database tables. As soon as I tried to open the database diagram I faced following error. For a while I could not figure out how to resolve the error.

Error:
Database diagram support objects cannot be installed because this database does not have a valid owner. To continue, first use the Files page of the Database Properties dialog box or the ALTER AUTHORIZATION statement to set the database owner to a valid login, then add the database diagram support objects.

Workaround / Fix / Solution :

Well for a while I attempted few things and nothing worked. After that I carefully read the error and I realized that solution was proposed in the error only. I just have to read it carefully. Here are the steps I did to make this work.

-- Replace YourDatabaseName in following script
ALTER AUTHORIZATION ON DATABASE::YourDatabaseName TO sa
GO

  • Select your database >> Right Click >> Select Properties
  • Select FILE in left side of page
  • In the OWNER box, select button which has three dots (…) in it
  • Now select user ‘sa’ or NT AUTHORITY\SYSTEM and click OK.

This should solve your problem.

Please note, I suggest you check your security policies before changing authorization. I did this to quickly solve my problem on my development server. If you are on production server, you may open yourself to potential security compromise.

Reference: Pinal Dave (http://blog.sqlauthority.com)

Older Posts »

Follow

Get every new post delivered to your Inbox.

Join 16,067 other followers