Questions tagged [self-join]

A table can be joined to itself and this is called a self-join. You can use a self-join to create a result set that joins records in a table with other records in the same table for example to filter a specific subset of records from within a single table without returning duplicate or similar rows.

Self-joins are useful in queries or subqueries such as the following:

  • a limited result set of rows is needed
  • a relationship between rows is needed
  • a calculation among rows is needed

Self-joins are also useful when converting vendor specific syntax to vendor agnostic syntax

References

1264 questions
-2
votes
2 answers

SQL Select employees with work history in multiple states

I need a query that will show only employees who have been paid in more than one state. The query will pull three columns: EmployeeID WorkLocation LastPayDate My current, unsuccessful attempt: Select EmployeeID , WorkLocation ,…
-3
votes
3 answers

Join Statements

Please explain the SQL query given below. I m new with SQL and struggling with Join Statements. Thanks in advance. select a.ename as attendee , t.ename as trainer from employees t join offerings o on (o.trainer…
Abhinav
  • 3
  • 2
-3
votes
0 answers

How to get MySQL delta diff with group by from same table

I am having a Mysql table item_details as below and want to get the delta diff for same item from next row sale_count. item_id,sale_count 10001,32 10001,43 10001,50 10005,34 10005,48 10007,82 10007,82 Need result as…
agarwal_achhnera
  • 2,582
  • 9
  • 55
  • 84
-3
votes
1 answer

How can I combine two virtual tables from one table, assigning half the data to a new column, without losing data

I have a table formatted as such age height name 15 180 george 16 192 phil 20 148 lily 17 187 george 19 196 phil 24 147 lily 19 190 george 20 199 phil 22 148 lily 21 190 george 27 197 phil 60 138 lily I need to get…
AlbinoRhino
  • 467
  • 6
  • 23
-3
votes
2 answers

Check condition within table possibly self-join?

I have a users table with many columns however I am trying to use the below 2 columns to fetch the users do not have expired = 1 The query should return only user 2. Any help is much appreciated. user_id expired 1 1 1 0 2 …
Rocky3151
  • 67
  • 7
-3
votes
2 answers

sql join values from same table

i came up by visiting this question. so i made a query like this SELECT ru.iUserId,ru.iRefUserId,CONCAT(ru.vName,ru.vLastName) AS userName,CONCAT(iru.vName,iru.vLastName) AS RefUserName FROM `register_user` ru INNER JOIN `register_user` iru ON…
TarangP
  • 2,711
  • 5
  • 20
  • 41
-3
votes
2 answers

how do we use SQL server Self join for the following situation?

I have a table Transactions, having following columns: txn_id, Account_id, Amount, txn_id, txn_type_id txn_type_id is the column which shows whether the transaction was CDT or DBT, using self join I want to present the records to show the CDT and…
Zia Ullah
  • 93
  • 1
  • 1
  • 8
-3
votes
1 answer

Select all passengers on the same flight

I need to find the users that are on the same flight, the tables that hold the specific information are: person(person_id, name, dob, address) passenger (passenger_ID, person_ID) ticket ( ticket_num(PK), passenger_id(FK), flight_num(FK) ) my…
Artemis
  • 123
  • 1
  • 8
-3
votes
3 answers

List the names of all married employees and their spouses who are assigned to the “East London Crossing” project

Project Table Pk(project Number) Marriage Table : PK (EmployeeNumber) Fk_ Marriage - employee (spouseNumber) Employee Table (EmployeeNumber) Workon Table : PK(EmployeeNumber,ProjectNumber,CityCode) FK_ Workon - employee…
user8688087
-3
votes
2 answers

How can I speed up my Moodle mysql query with multiple self joins and index

I have created a query with multiple joins for data fetching from multiple tables. For 5000+ records it is working very slow. I had checked and indexes are properly set up for each table used in a query. I tried to optimize this query with multiple…
-3
votes
1 answer

JAVA: How to join a HashTable with itself?

what is the most elegant way to join two Hashtables? I have this HashTable and I cannot use an Array or List or Set instead, because it messes up dealing with the custom-class. At least that's what I think. **Hashtable
ld4795
  • 7
  • 2
-3
votes
1 answer

Sql Developer - I am very new (Learning some basics)

I've very new to SQL Developer and I have some practice business problems to help build my knowledge.. What im trying to do is I have a table called 'VMStaff' with columns of Name, Department, Favourite Colour, Gender, Job Role and Manger…
-3
votes
1 answer

How to write SELF JOIN on the same table in mysql

I have a table named "comment", like bellow : c_id | c_action | r_c_id ----------------------------- 1 | post | NULL 2 | post | NULL 3 | post | NULL 4 | post | NULL 5 | reply | 1 6 | reply | 1 7 …
-3
votes
1 answer

SQL SERVER 2008 calculated field query

I have been trying to write this complex query where I have to calculate a total FTE by prganization by profession by year . So e.g. in org1 for 2011 we have 2 DataEntryC whose total FTE = 50.95+70.95 but now i also have to add 20% of admin staff…
user3844877
  • 493
  • 4
  • 9
  • 18
-4
votes
1 answer

Get data for past 6 months for each date using SQL

I am new to Sql and have a question. I have a table as follows. I want to write a query that will give me for every Month field go back 6 months and count the number of occurrences of that ID. For eg: something like this. How can we do this in…
1 2 3
84
85