What is a self join? Explain it with an example?
Answers were Sorted based on User's Feedback
Answer / ganesh
A join joins with itself is called self join
Working with self joins we use Alias tables
| Is This Answer Correct ? | 96 Yes | 21 No |
Answer / dhivya
lets take a table A which is joined to itself with its
alias is said to be self join.Most familiar example is
finding the manager of the employee..
select e1.emp_id manager_id,e2.emp_id employee_id from
employee e1,employee e2
where e1.emp_id=e2.emp_id
| Is This Answer Correct ? | 66 Yes | 37 No |
Answer / sanchit aggarwal(oracle dev.)
CREATE TABLE EMPLOYEE(
[EMPLOYEE_ID] INT PRIMARY KEY,
[NAME] NVARCHAR(50),
[MANAGER_ID] INT
)
GO
INSERT INTO EMPLOYEE VALUES(101,'Mary',102)
INSERT INTO EMPLOYEE VALUES(102,'Ravi',NULL)
INSERT INTO EMPLOYEE VALUES(103,'Raj',102)
INSERT INTO EMPLOYEE VALUES(104,'Pete',103)
INSERT INTO EMPLOYEE VALUES(105,'Prasad',103)
INSERT INTO EMPLOYEE VALUES(106,'Ben',103)
select e1.name from employee e1,employee e2
where e1.employee_id = e2.manager_id
| Is This Answer Correct ? | 31 Yes | 12 No |
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 |
Answer / madhavi
Joining the table to itself is self join.
Example:
To find the manager name for each employee in the employee
table:
select e1.empno employee_id,e1.ename employee_name,e2.ename
manager_name,e2.empno employee_num from emp e1,emp e2 where
e1.empno=e2.mgr;
| Is This Answer Correct ? | 27 Yes | 17 No |
For this Answer 1 is incorrect.
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.candytest from
candytest c1,candytest c2
where c1.candycolor=c2.candycolor
and c1.kidid=c2.kidid
| Is This Answer Correct ? | 20 Yes | 13 No |
Answer / kannan
CREATE TABLE IF NOT EXISTS `emp` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) collate latin1_general_ci NOT NULL,
`dept_id` int(11) NOT NULL,
`mgr_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
SELECT e2.name Employee_name, e1.name Manager_name
FROM emp e1, emp e2
WHERE e2.mgr_id = e1.id
| Is This Answer Correct ? | 12 Yes | 8 No |
Answer / anuj barthwal
A self join applies on one table only
Example:
emp_id emp_name emp_manager_id
1 Anuj NULL
2 Sunny 1
3 Suraj 1
4 Narban 2
if we want to get the name of the managers
select e1.emp_name 'manager',e2.emp_name 'employee'
from employees e1 join employees e2
on e1.emp_id=e2.emp_manager_id
manager employee
Anuj Sunny
Anuj Suraj
Sunny Narban
Self join means joining the single table to itself
| Is This Answer Correct ? | 5 Yes | 2 No |
Answer / sushant more (sybase dba)
CREATE TABLE EMPLOYEE(
[EMPLOYEE_ID] INT PRIMARY KEY,
[NAME] NVARCHAR(50),
[MANAGER_ID] INT
)
GO
INSERT INTO EMPLOYEE VALUES(101,'Mary',102)
INSERT INTO EMPLOYEE VALUES(102,'Ravi',NULL)
INSERT INTO EMPLOYEE VALUES(103,'Raj',102)
INSERT INTO EMPLOYEE VALUES(104,'Pete',103)
INSERT INTO EMPLOYEE VALUES(105,'Prasad',103)
INSERT INTO EMPLOYEE VALUES(106,'Ben',103)
select e1.NAME from EMPLOYEE e1,EMPLOYEE e2
where e1.EMPLOYEE_ID = e2.MANAGER_ID
| Is This Answer Correct ? | 1 Yes | 0 No |
What are data modelling techniques?
What is an extended stored procedure?
What are the main phases of database development?
The count() function always returns a int value type what should you do if you need to count rows from a query which you know will return a value that is too large for an int value type?
What is bit datatype and what's the information that can be stored inside a bit column?
what is difference between primary key and unique key ?
A) Which two are benefits of Teradata's support for ANSI Standard SQL? (Choose two.) 1.data is distributed automatically 2.queries get optimized to better plans 3.submit queries from other database systems 4.can interface with BI tools B) Which statement is true when comparing the advantages of third normal form to star schema? 1.Star schema tends to have fewer entities. 2.Star schema requires additional data storage. 3.Third normal form tends to have fewer entities. 4.Third normal form requires additional data storage. C)Which two sets of functions does the Parsing Engine (PE) perform? (Select two.) 1.sorting, formatting, and aggregating of the final answer set 2.flow control of the data to and from the participating tables 3.SQL statement interpretation, syntax validation, and semantic evaluation 4.dispatching the step execution sequence to the AMP via the BYNET D) Which two can be achieved with Teradata Active System Management (TASM)? (Choose two.) 1.disable hardware 2.react to hardware failure 3.influence response times 4.collect metadata E) Which three mechanisms can be used to ensure security within the Teradata Database? (Choose three.) 1.views 2.spool limits 3.roles 4.access rights 5.profiles
What is normalization? Explain different levels of normalization?
8 Answers CG-VAK, Choice Solutions, College School Exams Tests, IBM, Wipro,
What is dbms and its application?
Explain the general syntax for a SELECT statements covering all the options.
What is DB Development software?
On friday, you issued several insert statements using query analyzer. You then verified the data had been correctly entered with a select statement on monday, your users report that the data is not there what happened?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)