Multiple select query with where condition
- combining two select statements in sql query
- join two select statements in sql server
- can you use multiple select statements in sql
- can you have multiple select statements in sql
How to combine two sql queries in one result without union...
Problem
You’d like to display data from given columns (of a similar data type) from two tables in SQL.
Example
There are two tables in our database: and .
The table contains data in the following columns: , , , and .
id | first_name | last_name | age |
---|---|---|---|
1 | Tom | Miller | 22 |
2 | John | Smith | 26 |
3 | Lisa | Williams | 30 |
4 | Charles | Davis | 21 |
5 | James | Moore | 22 |
The table contains data in the following columns: , , , and .
id | first_name | last_name | age |
---|---|---|---|
1 | Milan | Smith | 45 |
2 | Charles | Davis | 21 |
3 | Mark | Backer | 19 |
In one result set, let’s display the first name, last name, and age for all people in the database, both employees and customers.
Solution 1
We’ll use to combine data from columns in two tables.
Here’s the query you’d write:
SELECT first_name, last_name, age FROM employee UNION ALL SELECT first_name, last_name, age FROM customer;Here’s the result:
first_name | last_name | age |
---|---|---|
Tom | Miller | 22 |
John | Smith | 26 |
Lisa | Williams | 30 |
Charles | Davis | 21 |
James | Moore | 28 |
Milan | Smith | 45 |
Charles | Davis | 21 |
Mark | Ba
|