What is a self join? Explain it with an example?
Answer Posted / lalithg
CREATE TABLE candytest
(kidId char(2),
candycolor varchar(10)
)
GO
INSERT INTO candytest SELECT 'K1', 'Yellow'
INSERT INTO candytest SELECT 'K1', 'Red'
INSERT INTO candytest SELECT 'K2', 'Red'
INSERT INTO candytest SELECT 'K2', 'Blue'
INSERT INTO candytest SELECT 'K3', 'White'
INSERT INTO candytest SELECT 'K3', 'Red'
INSERT INTO candytest SELECT 'K3', 'Yellow'
Go
--Now to find those kidid's which has both yellow and red
colored candies, we can write query as:
SELECT c1.kidid
FROM candytest AS c1 JOIN candytest AS c2
ON c1.candycolor = 'Red' AND c2.candycolor = 'Yellow'
AND c1.kidid = c2.kidid
| Is This Answer Correct ? | 85 Yes | 69 No |
Post New Answer View All Answers
How long can an error message be in raiseerror function?
What are the constraints on severity level in raiseerror?
What is an application role and explain a scenario when you would use one?
Who are naive users?
What is data modeling with example?
What are the three basic rules which are to be followed for the relational model of the database?
Explain what a database is?
You are testing the performance of a query the first time you run the query, the performance is slow. the second time you run the query, the performance is fast. why is this?
What is DB Development software?
What are data modelling techniques?
Explain about relational operator join?
Explain about the hierarchical model of the database?
Where is dbms used?
How can you log an error to the server event log from a stored procedure?
Explain the rollup operator?