2

What would be the best way to combine the two SQL statements into one?

  SELECT MIN(capacity) AS mincapacity, MAX(capacity) AS maxcapacity 
  FROM room

  SELECT MIN(grade) AS mingrade, MAX(grade) AS maxgrade 
  FROM room_grade

Thanks in advance!

7
  • 1
    Which returned resultset schema are you expect? Commented Mar 22, 2012 at 16:00
  • is there a common key between the two tables? Commented Mar 22, 2012 at 16:00
  • 3
    Why do you want to combine those those statements into one? At first glance it looks like a bad idea... Commented Mar 22, 2012 at 16:01
  • I need to extract the mincapacity, maxcapacity, mingrade and maxgrade values from the table. Commented Mar 22, 2012 at 16:01
  • 2
    You haven't actually given the schema for the tables nor what you want to achive but joining these seems illogical as your just getting the min and max of two columns. Commented Mar 22, 2012 at 16:02

6 Answers 6

4

Given that both resultsets contain only one row each, the simplest way would be a cross join:

select * from
(SELECT MIN(capacity) AS mincapacity, MAX(capacity) AS maxcapacity FROM room) r
cross join
(SELECT MIN(grade) AS mingrade, MAX(grade) AS maxgrade FROM room_grade) g
Sign up to request clarification or add additional context in comments.

Comments

3
SELECT
  *
FROM
  (SELECT MIN(capacity) AS mincapacity, MAX(capacity) AS maxcapacity FROM room) AS room
CROSS JOIN
  (SELECT MIN(grade) AS mingrade, MAX(grade) AS maxgrade FROM room_grade) AS room_grade

The main benefits here are having all the records on one row, and not joining the component records of each table.

Answer that involve a join before the aggregation will introduce a significant overhead that will make a massive difference to performance.

3 Comments

Wow, I thought that CROSS JOIN is a perfromance killer for such tasks, where is a trick, in aggregation function?
@sll - Aggregate, then join. So you are joining 1 record to 1 record. Rather than joining n records to m records, then aggregating.
@MatBailie Can you take a look at stackoverflow.com/questions/39471531/…
1
SELECT 'CAPACITY', MIN(capacity) AS min, MAX(capacity) AS max 
FROM room
UNION
SELECT 'GRADE', MIN(grade) AS min, MAX(grade) AS max 
FROM room_grade

should do the job.

Comments

1

you could do this with horrible JOIN syntax:

SELECT MIN(capacity) AS mincapacity, MAX(capacity) AS maxcapacity, MIN(grade) AS mingrade, MAX(grade) AS maxgrade
  FROM room, room_grade;

But you should use proper JOIN syntax

SELECT MIN(r.capacity) AS mincapacity, MAX(r.capacity) AS maxcapacity
    , MIN(rg.grade) AS mingrade, MAX(rg.grade) AS maxgrade
  FROM room r
  JOIN room_grade rg
    ON r.key = rg.key; (whatever the key is that joins the two tables)

1 Comment

I wouldn't do this. The join adds an unecessary overhead, and will perform dramatically worse than the two separate queries. Also if records are present in one talbe but not the other this can change the results.
1

EITHER

SELECT MIN(capacity) AS mincapacity, MAX(capacity) AS maxcapacity    FROM room
UNION ALL
SELECT MIN(grade) AS mingrade, MAX(grade) AS maxgrade    FROM room_grade

OR

SELECT MIN(r.capacity) AS mincapacity, MAX(r.capacity) AS maxcapacity, 
       MIN(rg.grade) AS mingrade, MAX(rg.grade) AS maxgrade    
FROM room r
INNER JOIN room_grade rg ON r.Id = rg.room_id

4 Comments

The union is messy but fine, but the join will introduce a massive overhead. Also if records are present in one table but not the other this can change the results.
Which JOIN overhead do you mean?
When specifying an INNER join, each record needs checking for existance in the other table. Keeping the queries separate yields (at worst) a full table scan of each table in the order that it currently exists. But with a join, at best, you'll need a merge join on the tables. This is much higher than niavely scanning each table independantly.
Are you talking about MS Sql Server?
1

You would want to join on an ID field. I assume the 'room' table has a unique key field, and the room_grade could have that same field. Do a join on the two something like so.

SELECT MIN(r.capacity) AS mincapacity, MAX(r.capacity) AS maxcapacity, MIN(g.grade) AS mingrade, MAX(g.grade) AS maxgrade
FROM room r
inner join room_grade g
on room r.roomId = g.roomId

1 Comment

The join will introduce a massive overhead. Also if records are present in one table but not the other this can change the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.