MySQL: select all columns from one table and some from another
Codemia Knowledge Hub ·
Join the two tables and qualify the wildcard with a table alias: SELECT e.*, d.department returns every column from employees and only department from departments. You can also combine tables with sub-queries or UNION, but a JOIN is the right fit when the rows are related by a column.
SELECT basics
The basic SELECT format:
An asterisk (*) selects all columns:
Selecting e.* plus specific columns with a JOIN
A JOIN combines rows from two or more tables based on a related column. The most common types are INNER JOIN and LEFT JOIN.
INNER JOIN example
Take two tables:
employees- Stores employee information.departments- Stores department information.
To get all columns from employees and only the department name from departments:
LEFT JOIN example
A LEFT JOIN returns all records from the left table (employees) and the matched records from the right table (departments). Where there is no match, the right table's columns are NULL.
This includes every employee, even one with no matching department.
How the query works
- Aliases:
employeesanddepartmentsget the aliaseseanddfor readability. - Wildcard selector (*):
e.*selects all columns fromemployees. - Specific columns:
d.departmentselects only thedepartmentcolumn fromdepartments. - JOIN condition: the ON clause specifies how the tables relate (
e.dept_id = d.id).
| Concept | Description |
| SELECT Columns | Use the asterisk * to select all columns, or specify columns explicitly. |
| JOIN Types | INNER JOIN returns matching rows; LEFT JOIN returns all left-side rows. |
| Aliases | Tables and columns can be given temporary names for clarity. |
| ON Clause | Specifies how tables are related, ensuring relational data retrieval. |
Related reading
- MySQL Select Date Equal to Today having datetime as the data type
- MySQL Select minimum/maximum among two or more given values
- MySQL select one column DISTINCT, with corresponding other columns
- MySQL SELECT only not null values
- MySQL Select Query - Get only first 10 characters of a value
- MySQL SELECT statement for the length of the field is greater than 1
- MySQL select where column is not empty
- MySQL SELECT WHERE datetime matches day and not necessarily time
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.