You use a self join when a table references data in itself.
E.g., an Employee
table may have a SupervisorID
column that points to the employee that is the boss of the current employee.
To query the data and get information for both people in one row, you could self join like this:
select e1.EmployeeID, e1.FirstName, e1.LastName, e1.SupervisorID, e2.FirstName as SupervisorFirstName, e2.LastName as SupervisorLastName from Employee e1 left outer join Employee e2 on e1.SupervisorID = e2.EmployeeID
Related Posts:
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- What is the difference between “INNER JOIN” and “OUTER JOIN”?
- SQL WITH clause example [duplicate]
- The wait operation timed out. ASP
- Conversion failed when converting date and/or time from character string while inserting datetime
- how to fix oracle ORA-01722 invalid number error
- MySQL – Operand should contain 1 column(s)
- SQL SELECT WHERE field contains words
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- NOT IN vs NOT EXISTS
- ORA-00979 not a group by expression
- Using group by on multiple columns
- Efficiently convert rows to columns in sql server
- How can I do a FULL OUTER JOIN in MySQL?
- Nested select statement in SQL Server
- How do I escape a single quote in SQL Server?
- What’s the difference between VARCHAR and CHAR?
- T-SQL split string based on delimiter
- Finding duplicate values in a SQL table
- MySQL Cannot Add Foreign Key Constraint
- Oracle error : ORA-00905: Missing keyword
- How to split the name string in mysql?
- How do I import an SQL file using the command line in MySQL?
- Sql Server equivalent of a COUNTIF aggregate function
- Column name or number of supplied values does not match table definition
- How do I do multiple CASE WHEN conditions using SQL Server 2008?
- Simple PHP Pagination script
- Difference between JOIN and INNER JOIN
- ORA-12560: TNS:protocol adaptor error
- What is the difference between JOIN and UNION?
- MySQL Multiple Joins in one query?
- “CASE” statement within “WHERE” clause in SQL Server 2008
- ORA-00972 identifier is too long alias column name
- What is the difference between Scope_Identity(), Identity(), @@Identity, and Ident_Current()?
- SQL – HAVING vs. WHERE
- What’s the difference between RANK() and DENSE_RANK() functions in oracle?
- Combining “LIKE” and “IN” for SQL Server
- Oracle – ORA-01489: result of string concatenation is too long [duplicate]
- SQL Server WITH statement
- How to SUM two fields within an SQL query
- “ORA-01438: value larger than specified precision allowed for this column” when inserting 3
- Algebra Relational sql GROUP BY SORT BY ORDER BY
- Oracle “Partition By” Keyword
- ERROR: there is no unique constraint matching given keys for referenced table “bar”
- What is the equivalent of ‘describe table’ in SQL Server?
- How do I query for all dates greater than a certain date in SQL Server?
- Add a column with a default value to an existing table in SQL Server
- Cast from VARCHAR to INT – MySQL
- SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO [duplicate]
- Using group by on multiple columns
- What is the purpose of using WHERE 1=1 in SQL statements?
- Conversion failed when converting the varchar value ‘simple, ‘ to data type int
- postgresql: error duplicate key value violates unique constraint
- Is there a workaround for ORA-01795: maximum number of expressions in a list is 1000 error?
- How do you force mysql LIKE to be case sensitive?
- ORA-06502: PL/SQL: numeric or value error: character string buffer too small
- What is the difference between a stored procedure and a view?
- Bulk load data conversion error (truncation)
- How to order by with union in SQL?
- Update statement with inner join on Oracle
- Can I use multiple “with”?
- PostgreSQL create table if not exists
- Incorrect syntax near ”
- MySQL: Can’t create table (errno: 150)
- Alternative to except in MySQL
- DATEDIFF function in Oracle
- GROUP BY and COUNT using ActiveRecord
- MySQL – Get row number on select
- Arithmetic overflow error converting varchar to data type numeric. ’10’ <= 9.00
- Difference between EXISTS and IN in SQL?
- What does * mean in sql?
- How to truncate the text returned for a column in a MySQL query
- INSERT VALUES WHERE NOT EXISTS
- Using union and order by clause in mysql
- Difference between `load data inpath ` and `location` in hive?
- How to implement LIMIT with SQL Server? [duplicate]
- What’s the best way to dedupe a table?
- Moving database with phpMyAdmin
- How To Write An Inner Join With WP Query
- How do I escape a table name or column name in SQL? esc_sql doesn’t do this
- Update user_login, user_nicename, and display_name
- Change sticky status of posts from phpMyAdmin
- Fetch all Posts where logged in user has commented
- Best Way to Merge a Dev and Live Site to Become a Staging Site?
- Needing to move content from postmeta to posts in sql [closed]
- How to import a Typo3 database to a wordpress site?
- Clean up very big and very dirty database
- How to delete ALL comments from certain category in WordPress database?
- How can I convert everything from category X to have post format Link
- Bulk delete WordPress Post and all metadata, etc using SQL query
- What steps do I need to take to install a local copy of a live website?
- Reset post IDs with all post meta
- How to Add or Change Post Title
- Why does DROP TABLE-ing the `wp_options` reset my user session?
- How to refactor DB queries for better TTFB in WordPress?
- query sql-table and change entities
- Duplicate WP Migration affecting site on separate domain?
- Backtick (MySQL norm) added to SQL Server Query causing error
- SQL query to check whether a meta key is set or not for a post in post_meta table
- Firebase with WordPress instead of SQL?