Find Customer Referee

Write an SQL query to report the names of the customer that are not referred by the customer with id = 2. Return the result table in any order.

table name: Customers

Explanation:

We need to find details of all those customers whose referee_id is not equal to 2. It could be anything other than 2. It could be null also.

Solution:

select * from  Customers where referee_id is null or referee_id !=2

Output:


SQL Script:

CREATE TABLE [dbo].[Customers](
       [id][int] NOT NULL,
       [name][varchar](50) NULL,
       [referee_id][int] NULL,
 CONSTRAINT [PK_Customres1] PRIMARY KEY CLUSTERED
(
       [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[Customers] ([id], [name], [referee_id]) VALUES (1, N'Will', NULL)
GO
INSERT [dbo].[Customers] ([id], [name], [referee_id]) VALUES (2, N'Jane', NULL)
GO
INSERT [dbo].[Customers] ([id], [name], [referee_id]) VALUES (3, N'Alex', 2)
GO
INSERT [dbo].[Customers] ([id], [name], [referee_id]) VALUES (4, N'Bill', NULL)
GO
INSERT [dbo].[Customers] ([id], [name], [referee_id]) VALUES (5, N'Zack', 1)
GO
INSERT [dbo].[Customers] ([id], [name], [referee_id]) VALUES (6, N'Mark', 2)
GO


Comments (0)