Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:24:01+00:00 2026-05-13T08:24:01+00:00

I have a database for a Train company. I want to combine two queries

  • 0

I have a database for a Train company. I want to combine two queries together so I can use them as one from a Java front end.

The first query returns the IDs of trains that are not full.

select TrainID 
  from Train 
 where Capacity > 0;

Each train has a capacity of 50 and I subtract one each time a seat is booked, so if the capacity is greater than zero, then there’s a seat on board.

The second query returns the RouteID of a train given a destination and origin:

select * 
  from Timetable 
 where RouteID = (select RouteID 
                    from Routes 
                   where OriginID = "New York" 
                     and DestinationID = "LA");

The Question/Need

I want to merge these two queries together so I can have something like:

Give me all the Timetable entries for this route ONLY IF there's room on the train.

I’m not a DB kinda guy so I’m just having trouble putting two and two together.

Edit: Schema is outlined below. Thanks for the answers so far, I’m going to try them now.

Routes

+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| RouteID       | varchar(25) | NO   | PRI | NULL    |       |
| OriginID      | varchar(25) | NO   | MUL | NULL    |       |
| DestinationID | varchar(25) | NO   | MUL | NULL    |       |
| Duration      | int(3)      | NO   |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+

Stations

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| StationID | varchar(25) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

Timetable

+-------------+-------------+------+-----+---------------------+----------------+
| Field       | Type        | Null | Key | Default             | Extra          |
+-------------+-------------+------+-----+---------------------+----------------+
| TimeID      | int(11)     | NO   | PRI | NULL                | auto_increment |
| RouteID     | varchar(11) | NO   |     | NULL                |                |
| TrainID     | varchar(11) | NO   |     | NULL                |                |
| DepartDate  | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
| ArrivalDate | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+-------------+------+-----+---------------------+----------------+

Train

]+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| TrainID  | varchar(11) | NO   | PRI | NULL    |       |
| Capacity | int(11)     | NO   |     | 50      |       |
+----------+-------------+------+-----+---------+-------+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:24:02+00:00Added an answer on May 13, 2026 at 8:24 am

    Assuming there is a trainid in the timetable table this should be close

    select a.*
    from Timetable a
    inner join Train b on a.trainid = b.trainid and b.capacity > 0
    where a.RouteID =
         (
         select RouteID
         from Routes
         where OriginID = "New York" and DestinationID="LA"
         );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have database with two values id and val so i want to ask
I have database backups taken from 2 machines(same database). ( about 75 tables) One
I have database application, I want to allow the user to restore the deleted
I have database, and in one the tables I need change the values of
Currently I have database with the following associations: One Client to Many Intakes One
I am connected to a oracle database from a scala/lift webapp. I have been
I'm planning to use MNIST database to train a classifier to recognize the hand
I have database Paradox 7. Two tables: Order -------- OCode ODate // Buy date
I have database A and database B. I would like to do one way
I have two stored procedures that I want execute wrapped in a transaction. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.