NULLs and JOINs | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In our last article, we introduced the concept of the NULL (or “unknown”) value and described how SQL treats this value when encountered in logical operations such as AND and OR. In this article, we take a look at how NULL values impact a more complicated operation – the JOINing of two tables.
Let's begin by taking a look at two sample tables that comprise the course database for a school. We might first have a list of courses and instructors, as shown below. (Note that, for simplicity's sake, we are making the assumption that there is only one section of each course and, therefore, that we can use it as the primary key.) In this case, the instructor Adams has not yet been assigned to a course and the Computers course has no instructor assigned yet.
Now, let's assume that we have a table containing course registrations for students. Again, for simplicity's sake, we'll assume that each student must take one and only one course. Every student must take a class, so we will include a NULL record for any student whose course registration information is unknown. In this case, we're not sure what course Alan is taking.
If we perform an INNER JOIN, NULL values do not match each other. For example, if we perform the following query:
SELECT *
FROM Instructors INNER JOIN Students ON Instructors.Course = Students.Course
We'd get the following result:
The OUTER JOIN operations includes data from one or both tables that doesn't match data in the other table. There are three types of OUTER JOINs:
If we performed the following query:
SELECT *
FROM Instructors LEFT OUTER JOIN Students ON Instructors.Course = Students.Course
We'd get the following result set:
Similarly, the query:
SELECT *
FROM Instructors RIGHT OUTER JOIN Students ON Instructors.Course = Students.Course
Would give us:
And, finally, the query:
SELECT *
FROM Instructors FULL OUTER JOIN Students ON Instructors.Course = Students.Course
Would yield:
And that's a look at how NULL values are treated by the JOIN operation!
|
I would like to share my knowledge and open to recieve knowledge from others.
Tuesday, 29 January 2013
How to deal with null values in joins?
Join ,union and union all with null values in tables?
--demo of
join,union and union all
NOTE: Tables
having null values
create table join1(id int,name varchar(50))
insert into join1 values(1,'jana'),(2,'sundu'),(null,null),(4,'null')
create table join2(id int,name varchar(50))
insert into join2 values(1,'jana'),(2,null),(3,null),(4,'null')
select * from join1 j1
inner join
join2 j2 on
j1.id=j2.id
Result
id name id name
1 jana 1 jana
3 sundu 3 NULL
4 null 4 null
--union
select * from join1 j1
union
select * from join2 j2
--Note:union
will give both matching rows and nonmatching rows only once
--i.e distinct
values
Result
id name
NULL adi
1 jana
2 NULL
3 NULL
3 sundu
4 null
--union all
select * from join1 j1
union all
select * from join2 j2
--it gives all values from both
tables
Result
id name
1 jana
NULL adi
3 sundu
4 null
1 jana
2 NULL
3 NULL
4 null
Sunday, 27 January 2013
views in sql server?
Views are virtual tables that represent the result set of a select statement from one or more tables or other views. In most cases, that is unless a view is indexed, a view is basically a predefined query that is stored and executed whenever the view is referenced in a query.
| ||||||
USE AdventureWorks2008
GO
CREATE TABLE Employee
(EmpID int NOT NULL CONSTRAINT PK_EMP PRIMARY KEY CLUSTERED,
EmpFirstName Varchar(50),
EmpLastName Varchar(50),
EmpSSN Varchar(9),
DepartmentID int)
GO
CREATE TABLE Department
(DepartmentID int NOT NULL CONSTRAINT PK_DEPT PRIMARY KEY CLUSTERED,
DepartmentDscr Varchar(50))
GO
CREATE VIEW vMarketingEmployees
WITH ENCRYPTION
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
dbo.Employee.EmpLastName AS LastName,
dbo.Department.DepartmentDscr AS Department
FROM dbo.Department INNER JOIN
dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
WHERE dbo.Department.DepartmentID = 1
| ||||||
USE AdventureWorks2008
GO
ALTER VIEW vMarketingEmployees
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
dbo.Employee.EmpLastName AS LastName,
dbo.Department.DepartmentDscr AS Department
FROM dbo.Department INNER JOIN
dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
WHERE dbo.Department.DepartmentID = 2
| ||||||
DROP VIEW vMarketingEmployees
| ||||||
CREATE VIEW vDistributedSample
AS
SELECT col1, col2, col3 FROM Server1.DBName.dbo.TableName
UNION ALL
SELECT col1, col2, col3 FROM Server2. DBName.dbo.TableName
UNION ALL
SELECT col1, col2, col3 FROM Server3.DBName.dbo.TableName
| ||||||
USE AdventureWorks2008
GO
--Drop the view if it currently exists
IF OBJECT_ID('dbo.vMarketingEmployees', 'V') IS NOT NULL
DROP VIEW dbo.vMarketingEmployees;
GO
--Create a view using the WITH CHECK option
CREATE VIEW vMarketingEmployees
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
dbo.Employee.EmpLastName AS LastName,
dbo.Department.DepartmentID,
dbo.Department.DepartmentDscr AS Department
FROM dbo.Department INNER JOIN
dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
WHERE dbo.Department.DepartmentID = 1
WITH CHECK OPTION
| ||||||
USE AdventureWorks2008
GO
CREATE VIEW vEmployees
WITH SCHEMABINDING
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
dbo.Employee.EmpLastName AS LastName,
dbo.Department.DepartmentID,
dbo.Department.DepartmentDscr AS Department
FROM dbo.Department INNER JOIN
dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
GO
--Create an index on the view
CREATE UNIQUE CLUSTERED INDEX IDX_vEmployee_Dept
ON vEmployees (DepartmentID);
GO
| ||||||
self join in sql server?
SQL SERVER – 2005 Explanation and Example – SELF JOIN
June 3, 2007 by pinaldave
A self-join is simply a normal SQL join that joins one table to itself. This is accomplished by using table name aliases to give each instance of the table a separate name. Joining a table to itself can be useful when you want to compare values in a column to other values in the same column. A join in which records from a table are combined with other records from the same table when there are matching values in the joined fields. A self-join can be an inner join or an outer join. A table is joined to itself based upon a field or combination of fields that have duplicate data in different records. The data-type of the inter-related columns must be of the same type or needs to cast them in same type.
When all of the data you require is contained within a single table, but data needed to extract is related to each other in the table itself. Examples of this type of data relate to Employee information, where the table may have both an Employee’s ID number for each record and also a field that displays the ID number of an Employee’s supervisor or manager. To retrieve the data tables are required to relate/join to itself.
Another example which can be tried on SQL SERVER 2005 sample database AdventureWorks is to find products that are supplied by more than one vendor. Please refer the sample database for table structure.
--SELF JOIN
CREATE TABLE Employee246(
EmployeeID INT PRIMARY KEY,
Name NVARCHAR(50),
ManagerID INT
)
INSERT INTO Employee246
SELECT 1, 'Mike', 3
UNION ALL
SELECT 2, 'David', 3
UNION ALL
SELECT 3, 'Roger', NULL
UNION ALL
SELECT 4, 'Marry',2
UNION ALL
SELECT 5, 'Joseph',2
UNION ALL
SELECT 7, 'Ben',2
select * from Employee246
select e2.Name,e1.Name as manager from Employee246 e1
inner join
Employee246 e2
on
e2.EmployeeID=e1.ManagerID
Thursday, 24 January 2013
Primary key and unique ikey and difference between them?
A UNIQUE constraint is similar to PRIMARY key, but you can have more than one UNIQUE constraint per table.
When you declare a UNIQUE constraint, SQL Server creates a UNIQUE index to speed up the process of searching for duplicates. In this case the index defaults to NONCLUSTERED index, because you can have only one CLUSTERED index per table.
* The number of UNIQUE constraints per table is limited by the number of indexes on the table i.e 249 NONCLUSTERED index and one possible CLUSTERED index.
Contrary to PRIMARY key UNIQUE constraints can accept NULL but just once. If the constraint is defined in a combination of fields, then every field can accept NULL and can have some values on them, as long as the combination values is unique.
Subscribe to:
Posts (Atom)