Subshells and database calls

In the database curriculum and the subject challenge, I have a question about it:

  • What is the purpose of placing a database call in a subshell? What would happen if the database call wasn’t inside a subshell?

Second, why is the MAJOR variable surrounded with parentheses?

  • MAJOR_ID=$($PSQL “SELECT major_id FROM majors WHERE major=‘$MAJOR’”

Now, you can query your database using the PSQL variable like this: $($PSQL "<query_here>"). The code in the parenthesis will run in a subshell, which is a separate bash process. Below the get major_id comment in your loop, create a MAJOR_ID variable. Set it equal to the result of a query that gets the major_id of the current MAJOR in the loop. Make sure to put your MAJOR variable in single quotes.

  1. Here’s an example of how it looks: MAJOR_ID=$($PSQL "<query_here>")

  2. For the query, you want to use the SELECT, FROM, and WHERE keywords

  3. Here’s an example of how the query part looks: SELECT <column_name> FROM <table_name> WHERE <condition>

  4. The condition you want is major='$MAJOR'

  5. Here’s how the query should look: SELECT major_id FROM majors WHERE major='$MAJOR'

  6. Here’s how the whole line should look: MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")