Welcome to Part 2 of the Build a Student Database Lessons!
The first thing you need to do is start the terminal. Do that by clicking the "hamburger" menu at the top left of the screen, going to the "terminal" section, and clicking "new terminal". Once you open a new one, type echo hello SQL
into the terminal and press enter.
- Capitalization matters
- If the tests don't run automatically, try typing
exit
into the terminal and redoing the instructions
In Part 1 of this tutorial, you created a students
database and then a script to insert information about your computer science students into it. Log into the psql interactive terminal with psql --username=freecodecamp --dbname=postgres
to see if it's here.
- Type
psql --username=freecodecamp --dbname=postgres
into the terminal and press enter
List the databases.
- Use the list shortcut command
- It's the
\l
command - Type
\l
into the psql prompt and press enter - Enter
psql --username=freecodecamp --dbname=postgres
in the terminal to log into the psql prompt if you aren't already
Your database isn't here. You can use the .sql
file you created at the end of Part 1 to rebuild it. I recommend "splitting" the terminal. You can do that by clicking the "hamburger" menu at the top left of the window, going to the "Terminal" menu, and clicking "Split Terminal". Once you've done that, enter psql -U postgres < students.sql
in it to rebuild the database.
- Enter the suggested command in the terminal
- Make sure you are in the
project
folder first
A lot of stuff happened in the terminal. That looks promising. In the psql prompt, view the databases again.
- Use the list shortcut command
- It's the
\l
command - Type
\l
into the psql prompt and press enter - Enter
psql --username=freecodecamp --dbname=postgres
in the terminal to log into the psql prompt if you aren't already
There's your students
database. Connect to it.
- Use the connect shortcut command with the database name after it
- It's the
\c
command - Here's an example
\c <database>
- Type
\c students
into the psql prompt and press enter - Enter
psql --username=freecodecamp --dbname=postgres
in the terminal to log into the psql prompt if you aren't already
Now that you're connected. Display the tables and relations that are here to see if it's all correct.
- Use the display shortcut command
- It's the
\d
command - Type
\d
into the psql prompt and press enter - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That all looks right. View the details of the students
table to make sure the stucture is right.
- Use the display shortcut command with the table name after it
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d students
into the psql prompt and press enter - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Looks good. Make sure all the data is in the table, as well.
- View all the data in the
students
table - Use the
SELECT
andFROM
keywords with*
to view all the columns - Here's an example:
SELECT <columns> FROM <table_name>
; - Type
SELECT * FROM students;
into the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The data is all there. You should take a look at the details of the other tables and the data in them to make sure they look good. When you are done, use touch
in the bash terminal to create student_info.sh
. You are going to make a script to print info about your students.
- Here's an example:
touch <filename>
- Enter
touch student_info.sh
in the terminal - The bash terminal, not the psql one
- Make sure you are in the
project
folder first
Give your new file executable permissions.
- It's the
chmod
command with the+x
flag - Here's an example:
chmod +x <filename>
- Type
chmod +x student_info.sh
in the terminal and press enter
Add a shebang that uses bash at the top of your new script.
- The shebang you want is
#!/bin/bash
- Add
#!/bin/bash
to yourstudent_info.sh
file
Below the shebang, add a comment that says Info about my computer science students from students database
.
- Make sure it's a single line comment
- A comment look like this:
# <comment>
- Add
# Info about my computer science students from students database
below the "shebang" in yourstudent_info.sh
file
In the new script, use echo
to print ~~ My Computer Science Students ~~
. Use the -e
flag with it to put a new line at the beginning and end of the text.
- The new line character is
\n
- Here's an example:
echo -e "\n<text>\n"
- Add
echo -e "\n~~ My Computer Science Students ~~\n"
below the comment in yourstudent_info.sh
file
Run the script to make sure it's working.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
You will want to query the database again to get info about the students to display. Add the same PSQL
variable you use in your insert_data.sh
script. It looked like this: PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
- Add the suggested variable at the bottom of the
student_info.sh
file
Below the PSQL variable you just added, use echo
to print First name, last name, and GPA of students with a 4.0 GPA:
. Use the -e
flag to put a new line at the beginning of the sentence.
- The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nFirst name, last name, and GPA of students with a 4.0 GPA:"
You will want to print what that sentence is asking for. You should know how to make that query, but lets practice a little first. SQL
stands for "Structured Query Language". It's the language you have been using to manage your relational databases. In the psql prompt, view all the data in the students table like you have done many times.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You should look at the column titles that were returned. The *
gets all columns in a table with your query. You can return specific columns by putting the column name in the query instead of *
. In the psql prompt, view just the first_name
column from the students
table.
- Use the
SELECT
andFROM
keywords - Here's an example:
SELECT <column_name> FROM <table_name>;
- Enter
SELECT first_name FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Just the first_name
column was returned that time. You can specify as many columns you want returned by separating them with commas. View the first_name
, last_name
and gpa
columns from the students
table.
- Use the
SELECT
andFROM
keywords - Here's an example:
SELECT <column_1>, <column_2>, FROM <table_name>;
- Enter
SELECT first_name, last_name, gpa FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already - Don't filter any rows
You can return only rows you want by adding WHERE <condition>
to your query. A condition can consist of a column, an operator, and a value. Use one of these to view the same columns as before but only rows WHERE gpa < 2.5
.
- Here's an example:
SELECT <columns> FROM <table_name> WHERE <condition>;
- The previous command you used was
SELECT first_name, last_name, gpa FROM students;
- The condition you want is
WHERE gpa < 2.5
- Enter
SELECT first_name, last_name, gpa FROM students WHERE gpa < 2.5;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The <
only return rows where the gpa
column was less than 2.5
. Some other operators are: <
, >
, <=
, >=
. View the same columns, but only rows for students with a gpa
greater than or equal to 3.8
.
- The greater than or equal to operator is
>=
- Here's an example:
SELECT <columns> FROM <table_name> WHERE <condition>;
- The previous command you used was
SELECT first_name, last_name, gpa FROM students WHERE gpa < 2.5;
- The condition you want here is
WHERE gpa >= 3.8
- Enter
SELECT first_name, last_name, gpa FROM students WHERE gpa >= 3.8;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That only returned students with a GPA of 3.8 or better. There's equal (=
) and not equal (!=
) operators as well. View the same columns for students that don't have a 4.0 gpa.
- Use the not equal (
!=
) operator - The previous command you used was
SELECT first_name, last_name, gpa FROM students WHERE gpa >= 3.8;
- The condition you want here is
WHERE gpa != 4.0
- Enter
SELECT first_name, last_name, gpa FROM students WHERE gpa != 4.0;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The right query will get you only the data you are looking for. Back in your student_info.sh
file, add an echo
command to the bottom that prints what the sentence above it asks for. Place double quotes around it like this: echo "$($PSQL "<query_here>")"
. This will make it so the output isn't all on one line.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - You previously used,
SELECT first_name, last_name, gpa FROM students WHERE gpa != 4.0;
in the psql prompt - The condition you want here is
WHERE gpa = 4.0
- Practice the query in the psql prompt to make sure it's getting what you want
- If you run your script, the last echo statement should print:
Casares|Hijo|4.0
Vanya|Hassanah|4.0
Dejon|Howell|4.0
- Add
echo "$($PSQL "SELECT first_name, last_name, gpa FROM students WHERE gpa = 4.0")"
to the bottom of thestudent_info.sh
file
Run the script to see your students with the highest GPA's.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Add another echo
statement at the bottom of the script. Make it print All course names whose first letter is before 'D' in the alphabet:
. Put a new line in front of it like the first sentence.
- Use
echo
with the-e
flag and a new line character - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nAll course names whose first letter is before 'D' in the alphabet:"
Practice first. In the psql prompt, view all the data in the majors
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The operators you used with numbers in the last section can be used on text as well. Use the =
to view all majors named Game Design
. Don't forget that You need single quotes around text values.
- Use the
SELECT
,FROM
, andWHERE
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
major = 'Game Design'
- Enter
SELECT * FROM majors WHERE major = 'Game Design';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Next, view all the rows not equal to Game Design
.
- The not equal operator is
!=
- Use the
SELECT
,FROM
, andWHERE
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
major != 'Game Design'
- Enter
SELECT * FROM majors WHERE major != 'Game Design';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Use the greater than operator to see majors that come after it alphabetically.
- The greater than operator is
>
- You want to see what rows are
> 'Game Design'
- Use the
SELECT
,FROM
, andWHERE
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
major > 'Game Design'
- Enter
SELECT * FROM majors WHERE major > 'Game Design';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Game Design
was not included in the results because it is not > 'Game Design'
. Try it with the greater than or equal to operator.
- The greater than or equal to operator is
>=
- You want to see what rows are
>= 'Game Design'
- Use the
SELECT
,FROM
, andWHERE
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
major >= 'Game Design'
- Enter
SELECT * FROM majors WHERE major >= 'Game Design';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It included Game Design
in the results that time. So if you want to see results that start with a G
or after, you could use major >= 'G'
. View the majors that come before G
.
- Use the less than (
<
) operator to see rows that come beforeG
- Use the
SELECT
,FROM
, andWHERE
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
major < 'G'
- Enter
SELECT * FROM majors WHERE major < 'G';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
In your script, add an echo
at the bottom to print the suggested info like you did before. Make sure to use double quotes where needed.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - You previously used,
SELECT * FROM majors WHERE major < 'G';
in the psql prompt - The condition you want here is
WHERE course < 'D'
- You only want to get the
course
column from thecourses
table - Practice the query in the psql prompt to make sure it's getting what you want
- If you run your script, the last echo statement should print:
Computer Networks
Computer Systems
Artificial Intelligence
Calculus
Algorithms
- Add
echo "$($PSQL "SELECT course FROM courses WHERE course < 'D'")"
to the bottom of thestudent_info.sh
file
Run the script to see what course names come before the letter D
.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Looks like there is five of them. Add another sentence like the others that says: First name, last name, and GPA of students whose last name begins with an 'R' or after and have a GPA greater than 3.8 or less than 2.0:
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nFirst name, last name, and GPA of students whose last name begins with an 'R' or after and have a GPA greater than 3.8 or less than 2.0:"
To find that, start by using the psql prompt to view all the data in the students
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It returned 31 rows. Use the same command, but only return the rows for students whose last name comes before M
in the alphabet.
- Use the less than (
<
) operator to see rows that come beforeM
- Use the
SELECT
,FROM
, andWHERE
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
last_name < 'M'
- Enter
SELECT * FROM students WHERE last_name < 'M';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That returned 18 rows. You can use multiple conditions after WHERE
with AND
or OR
, among others. Just add the keyword and another condition. In the psql prompt, use the same command as before, but add an OR
to also return rows of students with a 3.9 GPA.
- The previous command was:
SELECT * FROM students WHERE last_name < 'M';
- Here's an example of the
WHERE
part:WHERE <condition_1> OR <condition_2>
- Add an
OR <condition>
to the previous command - The condition you want to add is
OR gpa = 3.9
- The whole condition is
WHERE last_name < 'M' OR gpa = 3.9
- Enter
SELECT * FROM students WHERE last_name < 'M' OR gpa = 3.9;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It showed rows where one of the conditions was true, there was one more than last time. Enter the previous command, but use AND
to view only students that meet both conditions.
- The previous command was:
SELECT * FROM students WHERE last_name < 'M' OR gpa = 3.9;
- Here's an example of the
WHERE
part:WHERE <condition_1> AND <condition_2>
- Enter
SELECT * FROM students WHERE last_name < 'M' AND gpa = 3.9;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Now it only shows rows where both conditions are true, one person. Enter the previous command, but add a third condition of OR gpa < 2.3
.
- The previous command was:
SELECT * FROM students WHERE last_name < 'M' OR gpa = 3.9;
- Here's an example of the
WHERE
part:WHERE <condition_1> AND <condition_2> OR <condition_2>
- The conditions look like this:
WHERE last_name < 'M' AND gpa = 3.9 OR gpa < 2.3;
- Enter
SELECT * FROM students WHERE last_name < 'M' AND gpa = 3.9 OR gpa < 2.3;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
This showed all students whose GPA is less than 2.3 because the final OR
condition was true for them. It didn't matter what their last name started with. You can group conditions together with parenthesis like this: WHERE <condition_1> AND (<condition_2> OR <condition_2>)
. This would only return rows where <condition_1>
is true and one of the others is true. View students whose last name is before M
that have a GPA of 3.9 or less than 2.3.
- The previous command was:
SELECT * FROM students WHERE last_name < 'M' OR gpa = 3.9 OR gpa < 2.3;
- Enter the previous command but group your conditions with parenthesis to only view the suggested rows
- Enter
SELECT * FROM students WHERE last_name < 'M' AND (gpa = 3.9 OR gpa < 2.3);
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Two students meet those conditions. Back in the student info file, add an echo command at the bottom to print the suggested rows.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - You previously used
SELECT * FROM students WHERE last_name < 'M' AND (gpa = 3.9 OR gpa < 2.3);
in the psql prompt - Practice the query in the psql prompt to make sure it's getting what you want
- The conditions should be
last_name >= 'R' AND (gpa > 3.8 OR gpa < 2.0)
- If you run your script, the last echo statement should print:
Efren|Reilly|3.9
Mariana|Russel|1.8
Mehdi|Vandenberghe|1.9
- Add
echo "$($PSQL "SELECT first_name, last_name, gpa FROM students WHERE last_name >= 'R' AND (gpa > 3.8 OR gpa < 2.0)")"
to the bottom of thestudent_info.sh
file
Run the script to see the results.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Moving along. Add another echo
command, like the others, with a sentence that says: Last name of students whose last name contains a case insensitive 'sa' or have an 'r' as the second to last letter:
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nLast name of students whose last name contains a case insensitive 'sa' or have an 'r' as the second to last letter:"
Start by viewing everything from the courses
table in the psql prompt to see how you might be able to find this out.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There's a few that contain the word Algorithms
. You can use LIKE
to find patterns in text like this: WHERE <column> LIKE '<pattern>'
. An underscore (_
) in a pattern will return rows that have any character in that spot. View the rows in this table with a course name that matches the pattern '_lgorithms'
.
- Here's an example:
SELECT * FROM courses WHERE course LIKE '<pattern>';
- Enter
SELECT * FROM courses WHERE course LIKE '_lgorithms';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That pattern matched only rows that had exactly one character, followed by lgorithms
. Another pattern character is %
. It means anything can be there. To find names that start with W
, you could use W%
. View the courses that end in lgorithms
.
- Use
LIKE
and a pattern with%
to view the courses ending inlgorithms
- Here's an example:
SELECT * FROM courses WHERE course LIKE '<pattern>';
- The pattern you want is
%lgorithms
- Enter
SELECT * FROM courses WHERE course LIKE '%lgorithms';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It found two that time. Try viewing courses that start with Web
.
- Use
LIKE
and a pattern with%
to view the courses starting withWeb
- Here's an example:
SELECT * FROM courses WHERE course LIKE '<pattern>';
- The pattern you want is
Web%
- Enter
SELECT * FROM courses WHERE course LIKE 'Web%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Combine the two pattern matching characters to show courses that have a second letter of e
.
- Use
LIKE
and a pattern with_
and%
to view the courses whose second letter ise
- Here's an example:
SELECT * FROM courses WHERE course LIKE '<pattern>';
- Remember that the
_
will match any single character and%
will match any number of characters - The pattern you want is
_e%
- Enter
SELECT * FROM courses WHERE course LIKE '_e%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Nice job! Try viewing the courses with a space in their names.
- Use
LIKE
and a pattern with two%
to view the courses with a space - Here's an example:
SELECT * FROM courses WHERE course LIKE '<pattern>';
- The pattern you want is
% %
- Enter
SELECT * FROM courses WHERE course LIKE '% %';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There they are. You can use NOT LIKE
to find things that don't match a pattern. View courses that don't contain a space.
- Use
NOT LIKE
and a pattern with two%
's to view the courses without a space - Here's an example:
SELECT * FROM courses WHERE course NOT LIKE '<pattern>';
- The pattern you want is
% %
- Enter
SELECT * FROM courses WHERE course NOT LIKE '% %';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Five courses without a space. Try finding the ones that contain an A
.
- Use
LIKE
and a pattern with two%
's to view the courses containingA
- Here's an example:
SELECT * FROM courses WHERE course LIKE '<pattern>';
- The pattern you want is
%A%
- Enter
SELECT * FROM courses WHERE course LIKE '%A%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
6 rows. This showed all the courses with a capital A
. ILIKE
will ignore the case of the letters when matching. Use it to see the courses with an A
or a
.
- Use
ILIKE
and a pattern with two%
's to view the courses containingA
in any case - Here's an example:
SELECT * FROM courses WHERE course ILIKE '<pattern>';
- The pattern you want is
%A%
- Enter
SELECT * FROM courses WHERE course ILIKE '%A%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It found 11 rows that time. You can put NOT
in front of ILIKE
as well. Use it to see the courses that don't contain an A
or a
.
- Use
NOT ILIKE
and a pattern with two%
's to view the courses not containingA
in any case - Here's an example:
SELECT * FROM courses WHERE course NOT ILIKE '<pattern>';
- The pattern you want is
%A%
- Enter
SELECT * FROM courses WHERE course NOT ILIKE '%A%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You combine these like any other conditions. View the courses that don't have a capital or lowercase A
and have a space.
- Use two conditions, one with
NOT ILIKE
and one withLIKE
- Here's an example:
SELECT * FROM courses WHERE course NOT ILIKE '<pattern>' AND course LIKE <pattern> ;
- The two patterns you want are
%A%
and% %
- Enter
SELECT * FROM courses WHERE course NOT ILIKE '%A%' AND course LIKE '% %';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
In your student info script, add an echo
statement at the bottom like the other to print the results of the suggested query.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - You previously used
SELECT * FROM courses WHERE course NOT ILIKE '%A%' AND course LIKE '% %';
in the psql prompt - Practice the query in the psql prompt to make sure it's getting what you want
- The conditions should be
last_name ILIKE '%sa%' OR last_name LIKE %r_
- If you run your script, the last echo statement should print:
Gilbert
Savage
Saunders
Hilpert
Hassanah
- Add
echo "$($PSQL "SELECT last_name FROM students WHERE last_name ILIKE '%sa%' OR last_name LIKE '%r_'")"
to the bottom of thestudent_info.sh
file
Run the script to see the results.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Looks like five students meet those conditions. Add another echo
command at the bottom, like the others. Make this one say: First name, last name, and GPA of students who have not selected a major and either their first name begins with 'D' or they have a GPA greater than 3.0:
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nFirst name, last name, and GPA of students who have not selected a major and either their first name begins with 'D' or they have a GPA greater than 3.0:"
Start by looking at all the data in the students table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
All the fields that are empty or blank are null
. You can access them using IS NULL
as a condition like this: WHERE <column> IS NULL
. View the students who don't have a GPA.
- Use the
SELECT
,FROM
,WHERE
, andIS NULL
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
gpa IS NULL
- Enter
SELECT * FROM students WHERE gpa IS NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Inversely, you can use IS NOT NULL
to see rows that aren't null. View all the info on students that do have a GPA.
- Use the
SELECT
,FROM
,WHERE
, andIS NOT NULL
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
gpa IS NOT NULL
- Enter
SELECT * FROM students WHERE gpa IS NOT NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
View all the info on students who haven't chosen a major.
- A null
major_id
field means that student hasn't chosen a major - Use the
SELECT
,FROM
,WHERE
, andIS NULL
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition>;
- The condition you want is
major_id IS NULL
- Enter
SELECT * FROM students WHERE major_id IS NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
View the students who don't have a major, but don't include students without a GPA.
- Use the
SELECT
,FROM
,WHERE
,IS NULL
,AND
andIS NOT NULL
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition_1> AND <condition_2>;
- The condition you want is
major_id IS NULL AND gpa IS NOT NULL
- Enter
SELECT * FROM students WHERE major_id IS NULL AND gpa IS NOT NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
One more. View the students who don't have a major and gpa.
- Use the
SELECT
,FROM
,WHERE
,IS NULL
, andAND
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> WHERE <condition_1> AND <condition_2>;
- The condition you want is
major_id IS NULL AND gpa IS NULL
- Enter
SELECT * FROM students WHERE major_id IS NULL AND gpa IS NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
In your script, add an echo
command at the bottom to print the results the sentence is looking for.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - Practice the query in the psql prompt to make sure it's getting what you want
- You will need to use the
SELECT
,FROM
,WHERE
,IS NULL
,AND
,LIKE
, andOR
keywords - If you run your script, the last echo statement should print:
Noe|Savage|3.6
Danh|Nhung|2.4
Hugo|Duran|3.8
- You previously used
SELECT * FROM students WHERE last_name < 'M' AND (gpa = 3.9 OR gpa < 2.3);
in the psql prompt - Here's an example of the conditions you want:
WHERE <condition_1> AND (<condition_2> OR <condition_3>)
- The conditions should look like this:
WHERE major_id IS NULL AND (first_name LIKE 'D%' OR gpa > 3.0)
- Add
echo "$($PSQL "SELECT first_name, last_name, gpa FROM students WHERE major_id IS NULL AND (first_name LIKE 'D%' OR gpa > 3.0)")"
to the bottom of thestudent_info.sh
file
Run the script to see the students that meet those conditions.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
There's three of them. Add another sentence, like the others that says Course name of the first five courses, in reverse alphabetical order, that have an 'e' as the second letter or end with an 's':
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nCourse name of the first five courses, in reverse alphabetical order, that have an 'e' as the second letter or end with an 's':"
You can specify the order you want your results to be in by adding ORDER BY <column_name>
at the end of a query. In the psql prompt, view all the info in the students
table in order by the GPA's.
- Use the
SELECT
,FROM
,WHERE
, andORDER BY
keywords with*
to view the suggested rows - Here's an example:
SELECT <columns> FROM <table> ORDER BY <column>;
- You want to use
ORDER BY gpa
at the end of the query - Enter
SELECT * FROM students ORDER BY gpa;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That put the lowest GPA's at the top. When using ORDER BY
, it will be in ascending (ASC
) order by default. Add DESC
(descending) at the end of the last query to put the highest ones at the top.
- The last command was
SELECT * FROM students ORDER BY gpa;
- Add
DESC
to the end of the last command - Enter
SELECT * FROM students ORDER BY gpa DESC;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Now, the highest GPA's are at the top. You can add more columns to the order by separating them with a comma like this: ORDER BY <column_1>, <column_2>
. Any matching values in the first ordered column will then be ordered by the next. View all the student info with the highest GPA's at the top, and in alphabetical order by first_name
if the GPA's match.
- Here's an example:
SELECT <columns> FROM <table> ORDER BY <column_1> DESC, <column_2>;
- You want to use
ORDER BY gpa DESC, first_name
for the order - Enter
SELECT * FROM students ORDER BY gpa DESC, first_name;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Many times, you only want to return a certain number of rows. You can add LIMIT <number>
at the end of the query to only get the amount you want. View the students in the same order as the last command, but only return the first 10 rows.
- The last command was
SELECT * FROM students ORDER BY gpa DESC, first_name;
- Add
LIMIT 10
to the end of the last command - Enter
SELECT * FROM students ORDER BY gpa DESC, first_name LIMIT 10;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The order of the keywords in your query matters. You cannot put LIMIT
before ORDER BY
, or either of them before WHERE
. View the same number of students, in the same order, but don't get the ones who don't have a GPA.
- The last command was
SELECT * FROM students ORDER BY gpa DESC, first_name LIMIT 10;
- The keywords you want are
SELECT
,FROM
,WHERE
,IS NOT NULL
,ORDER BY
, andLIMIT
, in that order - Here's an example:
SELECT <columns> FROM <table> WHERE <condition> ORDER BY <column> LIMIT <number>;
- The condition you want is
gpa IS NOT NULL
- Enter
SELECT * FROM students WHERE gpa IS NOT NULL ORDER BY gpa DESC, first_name LIMIT 10;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
In your script, add the echo
command to print the rows the sentence is asking for.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - If you run your script, the last echo statement should print:
Web Programming
Web Applications
Server Administration
Network Security
Database Systems
- Practice the query in the psql prompt to make sure it's getting what you want
- You will need the
SELECT
,FROM
,WHERE
,LIKE
,OR
,ORDER BY
,DESC
, andLIMIT
keywords - Add
echo "$($PSQL "SELECT course FROM courses WHERE course LIKE '_e%' OR course LIKE '%s' ORDER BY course DESC LIMIT 5")"
to the bottom of thestudent_info.sh
file
Run the script to see the courses.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
😎 Add another echo
command at the bottom of the script like the others. Make this one say, Average GPA of all students rounded to two decimal places:
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nAverage GPA of all students rounded to two decimal places:"
There's a number of mathematic functions to use with numerical columns. One of them is MIN
, you can use it when selecting a column like this: SELECT MIN(<column>) FROM <table>
. It will find the lowest value in the column. In the psql prompt, view the lowest value in the gpa
column of the students
table.
- Enter
SELECT MIN(gpa) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Another one is MAX
, use it to see the largest gpa
of the same table.
- You previously used
SELECT MIN(gpa) FROM students;
- Enter
SELECT MAX(gpa) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
In the same fashion, use a SUM
function find out what all the values of the major_id
column in the students
table add up to.
- Use
SUM
like you usedMIN
andMAX
- You previously used
SELECT MAX(gpa) FROM students;
- Enter
SELECT SUM(major_id) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
AVG
will give you the average of all the values in a column. Use it to see the average of the same column.
- It's the
major_id
column in thestudents
table - Use
AVG
like you usedSUM
,MIN
andMAX
- You previously used
SELECT SUM(major_id) FROM students;
- Enter
SELECT AVG(major_id) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You can round decimals up or down to the nearest whole number with CEIL
and FLOOR
, respectively. Use CEIL
to round the average major_id
up to the nearest whole number. Here's an example: CEIL(<number_to_round>)
.
- Here's another example:
CEIL(<average_of_major_id's_here>)
- You previously used
SELECT AVG(major_id) FROM students;
to get the average - Put
AVG(major_id)
inside the parenthesis of theCEIL
function - Enter
SELECT CEIL(AVG(major_id)) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Or, you can round a number to the nearest whole number with ROUND
. Use it to round the average of the major_id
column to the nearest whole number.
- Here's an example:
ROUND(<average_of_major_id's_here>)
- You previously used
SELECT CEIL(AVG(major_id)) FROM students;
to round a number up - Put
AVG(major_id)
inside the parenthesis of theROUND
function - Enter
SELECT ROUND(AVG(major_id)) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You can round to a specific number of decimal places by adding a comma and number to ROUND
, like this: ROUND(<number_to_round>, <decimals_places>)
. Round the average of the major_id
to five decimal places.
- You previously used
SELECT ROUND(AVG(major_id)) FROM students;
to get the average - Put
AVG(major_id), 5
inside the parenthesis of theROUND
function - Enter
SELECT ROUND(AVG(major_id), 5) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You should be able to find what your script is asking for now. Add the command to print it.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - If you run your script, the last echo statement should print:
3.09
- Practice the query in the psql prompt to make sure it's getting what you want
- You previously used
SELECT ROUND(AVG(major_id), 5) FROM students;
in the psql prompt - Add
echo "$($PSQL "SELECT ROUND(AVG(gpa), 2) FROM students")"
to the bottom of thestudent_info.sh
file
Run the script to see the average GPA of all your students.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
They're doing pretty good. Add another command to print Major ID, total number of students in a column named 'number_of_students', and average GPA rounded to two decimal places in a column name 'average_gpa', for each major ID in the students table having a student count greater than 1:
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nMajor ID, total number of students in a column named 'number_of_students', and average GPA rounded to two decimal places in a column name 'average_gpa', for each major ID in the students table having a student count greater than 1:"
Another function is COUNT
. You can use it like this: COUNT(<column>)
. It will tell you how many entries are in a table for the column. Try it out in the psql prompt by using COUNT(*)
to see how many majors there are.
- Use the
SELECT
,COUNT
, andFROM
keywords - Here's an example
SELECT COUNT(<column>) FROM <table>;
- Use
*
for the column andmajors
for the table - Enter
SELECT COUNT(*) FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Using the same method, check how many students you have.
- You previously used:
SELECT COUNT(*) FROM majors;
- Use the
SELECT
,COUNT
, andFROM
keywords - Here's an example
SELECT COUNT(<column>) FROM <table>;
- Use
*
for the column andstudents
for the table - Enter
SELECT COUNT(*) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Using *
like that told you how many total rows are in the table. View the count of the major_id
column in the students
table to see how many of your students have picked a major.
- Use the
SELECT
,COUNT
, andFROM
keywords - Here's an example
SELECT COUNT(<column>) FROM <table>;
- Use
major_id
for the column andstudents
for the table - Enter
SELECT COUNT(major_id) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Using major_id
didn't count the null
values in that column. 23 students have a major. DISTINCT
is a function that will show you only unique values. You can use it like this: DISTINCT(<column>)
. View the unique major_id
values in the students
table.
- Use the
SELECT
,COUNT
, andFROM
keywords - Here's an example
SELECT DISTINCT(<column>) FROM <table>;
- Use
major_id
for the column andstudents
for the table - Enter
SELECT DISTINCT(major_id) FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There's six unique major_id
values in the students
table. You can get the same results with GROUP BY
. Here's an example of how to use it: SELECT <column> FROM <table> GROUP BY <column>
. Use this method to view the unique major_id
values in the students
table again.
- You want to select and group the
major_id
column - Use the
SELECT
,FROM
, andGROUP BY
keywords - Here's an example
SELECT COUNT(<column>) FROM <table>;
- Use
major_id
for the column andstudents
for the table - Enter
SELECT major_id FROM students GROUP BY major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The output was the same as DISTINCT
, but with GROUP BY
you can add any of the aggregate functions (MIN
, MAX
, COUNT
, etc) to it to find more information. For instance, if you wanted to see how many students were in each major you could use SELECT COUNT(*) FROM students GROUP BY major_id
. View the major_id
column and number of students in each major_id
.
- You want to
SELECT
two columns,major_id
and theCOUNT
of all (*
) the rows - Use the
SELECT
,COUNT
,FROM
, andGROUP BY
keywords - Here's an example
SELECT <column_1>, COUNT(<column_2>) FROM <table> GROUP BY <column_1>;
- Enter
SELECT major_id, COUNT(*) FROM students GROUP BY major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
When using GROUP BY
, any columns in the SELECT
area must be included in the GROUP BY
area. Other columns must be used with any of the aggregate functions (MAX
, AVG
, COUNT
, etc). View the unique major_id
values with GROUP BY
again, but see what the lowest GPA is in each of them.
- The last query was
SELECT major_id, COUNT(*) FROM students GROUP BY major_id;
- Use the
SELECT
,MIN
,FROM
, andGROUP BY
keywords - Here's an example
SELECT <column_1>, MIN(<column_2>) FROM <table> GROUP BY <column_1>;
- Enter
SELECT major_id, MIN(gpa) FROM students GROUP BY major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Nice job. Enter the same query, but add a column that shows you the highest GPA in each major as well.
- The last query was:
SELECT major_id, MIN(gpa) FROM students GROUP BY major_id;
- Use the
SELECT
,MIN
,MAX
,FROM
, andGROUP BY
keywords - Enter
SELECT major_id, MIN(gpa), MAX(gpa) FROM students GROUP BY major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Another option with GROUP BY
is HAVING
. You can add it at the end like this: SELECT <column> FROM <table> GROUP BY <column> HAVING <condition>
. The condition must be an aggregate function with a test. An example to might be to use HAVING COUNT(*) > 0
to only show what whatever column is grouped that have at least one row. Use HAVING
to only show rows from the last query that have a maximum GPA of 4.0.
- The last query was:
SELECT major_id, MIN(gpa), MAX(gpa) FROM students GROUP BY major_id;
- Use the
SELECT
,MIN
,MAX
,FROM
,GROUP BY
, andHAVING
keywords - Here's an example
SELECT <column_1>, MIN(<column>), MAX(<column>) FROM <table> GROUP BY <column_1> HAVING <condition>;
- The condition you want is
HAVING MAX(gpa) = 4.0
- Enter
SELECT major_id, MIN(gpa), MAX(gpa) FROM students GROUP BY major_id HAVING MAX(gpa) = 4.0;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Two of your majors have at least one student with a 4.0 GPA. Looking at the results, the column is named min
. You can rename a column with AS
like this: SELECT <column> AS <new_column_name>
Enter the same command, but rename the min
column to min_gpa
.
- The last query was:
SELECT major_id, MIN(gpa), MAX(gpa) FROM students GROUP BY major_id HAVING MAX(gpa) = 4.0;
- Use the
SELECT
,MIN
,AS
,FROM
, andGROUP BY
keywords - Rename the
MIN(gpa)
column like this:MIN(gpa) AS min_gpa
- Enter
SELECT major_id, MIN(gpa) AS min_gpa, MAX(gpa) FROM students GROUP BY major_id HAVING MAX(gpa) = 4.0;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Now the column has a better name. Enter the same command, but rename the max
column to max_gpa
as well.
- The last query was:
SELECT major_id, MIN(gpa) AS min_gpa, MAX(gpa) FROM students GROUP BY major_id HAVING MAX(gpa) = 4.0;
- Use the
SELECT
,MIN
,AS
,FROM
, andGROUP BY
keywords - Rename the
MAX(gpa)
column like this:MAX(gpa) AS max_gpa
- Enter
SELECT major_id, MIN(gpa) AS min_gpa, MAX(gpa) AS max_gpa FROM students GROUP BY major_id HAVING MAX(gpa) = 4.0;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That's more descriptive. View the major_id
and number of students in each major_id
in a column named number_of_students
.
- Use the
SELECT
,COUNT
,AS
,FROM
, andGROUP BY
keywords - Here's an example:
SELECT <column_1>, COUNT(*) AS <custom_column_name> FROM <table> GROUP BY <column_1>;
- You want to
COUNT(*) AS number_of_students
andGROUP BY major_id
- Enter
SELECT major_id, COUNT(*) AS number_of_students FROM students GROUP BY major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Use HAVING
with the last query to only show the rows with less than eight students in the major.
- The last query was:
SELECT major_id, COUNT(*) AS number_of_students FROM students GROUP BY major_id;
- Here's an example:
SELECT <column_1>, COUNT(*) AS <custom_column_name> FROM <table> GROUP BY <column_1> HAVING <condition>;
- The condition you want is
COUNT(*) < 8
- Enter
SELECT major_id, COUNT(*) AS number_of_students FROM students GROUP BY major_id HAVING COUNT(*) < 8;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Well done. Back in your script, add the command the print the suggested results.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - If you run your script, the last echo statement should print:
|8|2.97
37|6|3.38
36|6|2.92
41|6|3.53
38|4|2.73
- Practice the query in the psql prompt to make sure it's getting what you want
- You previously used
SELECT major_id, COUNT(*) AS number_of_students FROM students GROUP BY major_id HAVING COUNT(*) < 8;
in the psql prompt - Add
echo "$($PSQL "SELECT major_id, COUNT(*) AS number_of_students, ROUND(AVG(gpa),2) AS average_gpa FROM students GROUP BY major_id HAVING COUNT(*) > 1")"
to the bottom of thestudent_info.sh
file
Run the script to see the output.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Add an echo command to your script like the others that prints List of majors, in alphabetical order, that either no student is taking or has a student whose first name contains a case insensitive 'ma':
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nList of majors, in alphabetical order, that either no student is taking or has a student whose first name contains a case insensitive 'ma':"
The majors
and students
table are linked with the major_id
foreign key. If you want to see the name of a major that a student is taking, you need to JOIN
the two tables into one. Here's an example of how to do that:
SELECT * FROM <table_1> FULL JOIN <table_2> ON <table_1>.<foreign_key_column> = <table_2>.<foreign_key_column>;
In the psql prompt, join the two tables together with the above method.
- Join the
students
andmajors
table with the method in the example. Use thestudents
table first where applicable - Enter
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It's showing all the columns from both tables, the two major_id
columns are the same in each row for the ones that have it. You can see that there are some students without a major, and some majors without any students. The FULL JOIN
you used will include all rows from both tables, whether or not they have a row using that foreign key in the other. From there, you could use any of the previous methods to narrow down, group, order, etc. Use a LEFT JOIN
to join the same two tables in the same way.
- Join the
students
andmajors
table with aLEFT JOIN
. Use thestudents
table first where applicable - You previously entered:
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
- Replace
FULL JOIN
from the previous command withLEFT JOIN
- Enter
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There's a few less rows than the last query. In the LEFT JOIN
you used, the students
table was the left table since it was on the left side of the JOIN
. majors
was the right table. A LEFT JOIN
gets all rows from the left table, but only rows from the right table that are linked to from the left one. Looking at the data, you can see that every student was returned, but the majors without any students were not. Join the same two tables with a RIGHT JOIN
this time.
- Join the
students
andmajors
table with aRIGHT JOIN
. Use thestudents
table first where applicable - You previously entered:
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id;
- Replace
LEFT JOIN
from the previous command withRIGHT JOIN
- Enter
SELECT * FROM students RIGHT JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The right join showed all the rows from the right table (majors
), but only rows from the left table (students
) if they have a major. There's one more type you should know about. Join the two tables with an INNER JOIN
.
- Join the
students
andmajors
table with anINNER JOIN
. Use thestudents
table first where applicable - You previously entered:
SELECT * FROM students RIGHT JOIN majors ON students.major_id = majors.major_id;
- Replace
RIGHT JOIN
from the previous command withINNER JOIN
- Enter
SELECT * FROM students INNER JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
The INNER JOIN
only returned students if they have a major and majors that have a student. In other words, it only returned rows if they have a value in the foreign key column (major_id
) of the opposite table. You should know a little about the four main types of joins now. Try using a LEFT JOIN
to show all the majors but only students that have a major.
- You want to join the
students
andmajors
tables again - The left table is the on the left side of
LEFT JOIN
. - A
LEFT JOIN
will show all rows from the left table. - You previously entered:
SELECT * FROM students INNER JOIN majors ON students.major_id = majors.major_id;
- Enter
SELECT * FROM majors LEFT JOIN students ON majors.major_id = students.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Excellent. All the majors are there. Next, use the appropriate join to show only students that are enrolled in a major, and only majors that have a student enrolled in it.
- You want to join the
students
andmajors
tables again - Join them with the join that only shows rows if they have a value in the foreign key column of the other table
- The types of joins you learned are
FULL JOIN
,LEFT JOIN
,RIGHT JOIN
, andINNER JOIN
- The previous query was:
SELECT * FROM majors RIGHT JOIN students ON majors.major_id = students.major_id;
- You want to use an
INNER JOIN
with the two tables - Enter
SELECT * FROM majors INNER JOIN students ON majors.major_id = students.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
👍 Try using a right join to show all students but only majors if a student is enrolled in it.
- You want to join the
students
andmajors
tables again - The left table is the on the left side of
RIGHT JOIN
. - A
RIGHT JOIN
will show all rows from the right table. - You previously entered:
SELECT * FROM students INNER JOIN majors ON students.major_id = majors.major_id;
- Enter
SELECT * FROM majors RIGHT JOIN students ON majors.major_id = students.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That showed all the students since it was the right table of the RIGHT JOIN
. Use the appropriate join with the same two table to show all rows in both tables whether they have a value in the foreign key column or not.
- You want to join the
students
andmajors
tables again - Join them with the join that only shows rows if they have a value in the foreign key column of the other table
- The previous query was:
SELECT * FROM majors INNER JOIN students ON majors.major_id = students.major_id;
- You want to use an
INNER JOIN
with the two tables - Enter
SELECT * FROM majors FULL JOIN students ON majors.major_id = students.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Lets do some more experiments with joins. Say you wanted to find a list of majors that students are taking. Use the most efficient JOIN
to join the two tables you need. Only join the tables for now, don't use any other conditions.
- You want to join the
students
andmajors
tables again - Use the join that shows you only students that have a major and only majors that have a student.
- Only use the join, don't use a
WHERE
,HAVING
, or any other filters - You previously used:
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
- You want to use an
INNER JOIN
- Enter
SELECT * FROM students INNER JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Good. To get the list, you don't need all the columns, though. Enter the same command, but just get the column you need.
- The previous query was
SELECT * FROM students INNER JOIN majors ON students.major_id = majors.major_id;
- Enter the previous query, but only get the column you need
- You only need the
major
column - Enter
SELECT major FROM students INNER JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You also don't want any duplicates. Use DISTINCT
to only return the unique ones to see the list of majors who have students.
- The previous query was
SELECT major FROM students INNER JOIN majors ON students.major_id = majors.major_id;
- Enter the previous query, but only get the
DISTINCT
majors - Here's an example:
DISTINCT(<column>)
- You want to change
major
from the previous query toDISTINCT(major)
- Enter
SELECT DISTINCT(major) FROM students INNER JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There's your list of majors that students are taking 😄 Next, say you wanted a list of majors that students aren't taking. Use the most efficient JOIN
to join the two tables you need. Only join the tables for now, don't use any other conditions.
- You want to join the
students
andmajors
tables again - Use the join that shows you all majors, but only students that have a major
- Only use the join, don't use a
WHERE
,HAVING
, or any other filters - You previously used:
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
- You want to use a
RIGHT JOIN
with themajors
table on the right of it - Enter
SELECT * FROM students RIGHT JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
That got you all the majors, you can see the ones that don't have any students. Add a WHERE
condition to only see the majors without students, use student_id
in it's condition.
- The previous query was
SELECT * FROM students RIGHT JOIN majors ON students.major_id = majors.major_id;
- Enter the previous query, but add a
WHERE <condition>
at the end to only get the rows you need - Use
IS NULL
with the condition - The keywords you want are
SELECT
,FROM
,RIGHT JOIN
,ON
,WHERE
andIS NULL
- Use
student_id IS NULL
as the condition - Enter
SELECT * FROM students RIGHT JOIN majors ON students.major_id = majors.major_id WHERE student_id IS NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Now you only have the rows you need. Only get the columns you need with it to see the list of majors without students.
- The previous query was
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id WHERE student_id IS NULL;
- Enter the previous query, but only get the column you need
- The column you need is the
major
column - Enter
SELECT major FROM students RIGHT JOIN majors ON students.major_id = majors.major_id WHERE student_id IS NULL;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You're doing great. Next, use the most efficient 'JOIN' to join the tables you would need if you were asked to get the first name, last name, major, and GPA of students who are taking Data Science or have a gpa of 3.8 or greater. Only join the tables for now, don't use any other conditions.
- Use the join to get all students but only majors that have a student
- Only use the join, don't use a
WHERE
,HAVING
, or any other conditional expressions. - You previously used:
SELECT * FROM students RIGHT JOIN majors ON students.major_id = majors.major_id;
- You want to use a
LEFT JOIN
withstudents
as the left table - Enter
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Enter the same command, but use WHERE
to only get the students that meet the requirements. As a reminder, the goal was to find students who are taking Data Science or have a gpa of 3.8 or greater.
- The previous query was
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id;
- You want to add two conditions one testing the
major
column, and another testing thegpa
column - Here's an example
SELECT * FROM <table_1> LEFT JOIN <table_2> ON <table_1>.<foreign_key> = <table_2>.<foreign> WHERE <condition_1> OR <condition_2>;
- The two conditions you want are
major = 'Data Science'
andgpa >= 3.8
- Enter
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id WHERE major='Data Science' OR gpa >= 3.8;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Now, you have narrowed it down the rows you are looking for. Enter the same command, but only get the columns you need. There was four of them, the students first name, last name, their major, and GPA. Get them in that order.
- The previous query was
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id WHERE major='Data Science' OR gpa >= 3.8;
- Enter the previous query, but only get the columns you need
- Get the
first_name
,last_name
,major
, andgpa
columns in that order - Enter
SELECT first_name, last_name, major, gpa FROM students LEFT JOIN majors ON students.major_id = majors.major_id WHERE major='Data Science' OR gpa >= 3.8;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
From there, you could put them in a specific order if you wanted or limit the results to a certain number among other things. Lastly, use the most efficient 'JOIN' to join the tables you would need if you were asked to get the first name and major for students whose first_name
, or the major
, contains ri
. Only join the tables for now, don't use any other conditions.
- Use the join that gets all students and majors
- You previously used:
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id;
- You want to use a
FULL JOIN
- Enter
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Add a WHERE
to the previous query so you only get the rows you need. The rows you wanted were the ones with a first name or major containing ri
.
- The previous query was
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
- You want to add two conditions one testing the
first_name
column, and another testing themajor
column - Here's an example
SELECT * FROM <table_1> LEFT JOIN <table_2> ON <table_1>.<foreign_key> = <table_2>.<foreign> WHERE <condition_1> OR <condition_2>;
- The two conditions you want should use the
LIKE
orILIKE
keywords - They conditions are
WHERE first_name LIKE '%ri%' OR major LIKE '%ri%'
- Enter
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id WHERE first_name LIKE '%ri%' OR major LIKE '%ri%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Finally, you only wanted to display the first_name
and major
columns. Enter the previous query, but only get the columns you need.
- The previous query was
SELECT * FROM students LEFT JOIN majors ON students.major_id = majors.major_id WHERE first_name LIKE '%ri%' OR major LIKE '%ri%';
- The two columns you want are
first_name
andmajor
- Enter
SELECT first_name, major FROM students FULL JOIN majors ON students.major_id = majors.major_id WHERE first_name LIKE '%ri%' OR major LIKE '%ri%';
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
In your script, add the command to print what the sentence is asking for.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - If you run your script, the last echo statement should print:
Computer Programming
Database Administration
Network Engineering
Web Development
- Practice the query in the psql prompt to make sure it's getting what you want
- You previously used
SELECT first_name, major FROM students FULL JOIN majors ON students.major_id = majors.major_id WHERE first_name LIKE '%ri%' OR major LIKE '%ri%';
in the psql prompt - Add
echo "$($PSQL "SELECT major FROM students FULL JOIN majors ON students.major_id = majors.major_id WHERE major IS NOT NULL AND (student_id IS NULL OR first_name ILIKE '%ma%') ORDER BY major")"
to the bottom of thestudent_info.sh
file
Run the script to see the majors described.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
😄 Almost done. In your script, add a command to print this sentence like the others: List of unique courses, in reverse alphabetical order, that no student or 'Obie Hilpert' is taking:
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nList of unique courses, in reverse alphabetical order, that no student or 'Obie Hilpert' is taking:"
Lets go over a few more things before you figure out how to see the courses a student is taking. Start by doing a FULL JOIN
on your students
and majors
tables.
- Join the
students
andmajors
table with aFULL JOIN
. Use thestudents
table first where applicable - Enter
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
If you look at the column names, it shows two major_id
columns. One from the students
table and one from the majors
table. If you were to try and query it using major_id
, you would get an error. You would need to specify what table you want the column from like this: <table>.<column>
. Enter the same join but only get the major_id
column from the students
table.
- The previous query was
SELECT * FROM students FULL JOIN majors ON students.major_id = majors.major_id;
- You can get the column you want with
students.major_id
- Enter
SELECT students.major_id FROM students FULL JOIN majors ON students.major_id = majors.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Earlier, you used AS
to rename columns. You can use it to rename tables, or give them aliases, as well. Here's an example: SELECT * FROM <table> AS <new_name>;
. Enter the same query you just entered, but rename the majors
table to m
. Anywhere the majors
table is referenced, you will need to use m
instead of majors
.
- The previous query was
SELECT students.major_id FROM students FULL JOIN majors ON students.major_id = majors.major_id;
- You want to join
majors AS m
and usem.major_id
when referencing the joining column - Enter
SELECT students.major_id FROM students FULL JOIN majors AS m ON students.major_id = m.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
This doesn't affect the output. It can just make some queries easier to read. Enter the same query, but rename the students
table to s
as well.
- The previous query was
SELECT students.major_id FROM students FULL JOIN majors AS m ON students.major_id = m.major_id;
- You want to rename
students AS s
and uses.<column>
when referencing columns from thestudents
table - Enter
SELECT s.major_id FROM students AS s FULL JOIN majors AS m ON s.major_id = m.major_id;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There's a shortcut keyword, USING
to join tables if the foreign key column has the same name in both tables. Here's an example: SELECT * FROM <table_1> FULL JOIN <table_2> USING(<column>);
. Use this method to see all the columns in the students
and majors
table. Don't use any aliases.
- Enter
SELECT * FROM students FULL JOIN majors USING(major_id);
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Note that the two major_id
columns were turned into one with USING
. In order to find out what courses a student is taking, you will need to join all the tables together. You can add a third table to a join like this: SELECT * FROM <table_1> FULL JOIN <table_2> USING(<column>) FULL JOIN <table_3> USING(<column>)
. This example will join the first two tables into one, turning it into the left table for the second join. Use this method to join the two tables from the previous query with the majors_courses
table.
- The previous query was
SELECT * FROM students FULL JOIN majors USING(major_id);
- View the details of the
majors_courses
table with\d majors_courses
to find the foreign key to join on - It's the
major_id
column - Enter
SELECT * FROM students FULL JOIN majors USING(major_id) FULL JOIN majors_courses USING(major_id);
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You may need to adjust the terminal size to align the output. What you're seeing is every unique combination of rows in the database. Students with a major are listed multiple times, one for each course included in the major. The majors without any students are there along with the courses for them. The students without a major are included, they have no courses and are only listed once. You can join as many tables together as you want. Join the last table to the previous command to get the names of the courses with all this info.
- The previous query was
SELECT * FROM students FULL JOIN majors USING(major_id) FULL JOIN majors_courses USING(major_id);
- The last table is the
courses
table - View the details of the
courses
table with\d courses
to find the foreign key to join on - Enter
SELECT * FROM students FULL JOIN majors USING(major_id) FULL JOIN majors_courses USING(major_id) FULL JOIN courses USING(course_id);
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Same amount of rows, but you get the course names now. In your script, add the command to print the suggested info.
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - If you run your script, the last echo statement should print:
Web Programming
Web Applications
Python
Object-Oriented Programming
Network Security
Data Structures and Algorithms
Computer Systems
Computer Networks
Algorithms
- Practice the query in the psql prompt to make sure it's getting what you want
- You previously used
SELECT * FROM students FULL JOIN majors USING(major_id) FULL JOIN majors_courses USING(major_id) FULL JOIN courses USING(course_id);
in the psql prompt - Add
echo "$($PSQL "SELECT DISTINCT(course) FROM students RIGHT JOIN majors USING(major_id) INNER JOIN majors_courses USING(major_id) INNER JOIN courses USING(course_id) WHERE (first_name = 'Obie' AND last_name = 'Hilpert') OR student_id IS NULL ORDER BY course DESC")"
to the bottom of thestudent_info.sh
file
Run the script to see courses described.
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Last one. Add a command that prints List of courses, in alphabetical order, with only one student enrolled:
.
- At the bottom of the file, use
echo
with the-e
flag and a new line character again to print the suggested sentence - The new line character is
\n
- Here's an example of the command:
echo -e "\n<text_here>"
- At the bottom of the
student_info.sh
file, add this:
echo -e "\nList of courses, in alphabetical order, with only one student enrolled:"
Go for it.
- Add a command at the bottom of the script to print the suggested information
- Add
echo "$($PSQL "<query_here>")"
to the bottom of thestudent_info.sh
file, except with the correct query in it - If you run your script, the last echo statement should print:
Computer Networks
Computer Systems
Server Administration
UNIX
- Practice the query in the psql prompt to make sure it's getting what you want
- You can do this
- Give it another try
- I don't know how to get it either
- So there's no answers here
- 😕
- Try entering this in the psql prompt:
SELECT COUNT(course), COURSE FROM students INNER JOIN majors USING(major_id) INNER JOIN majors_courses USING(major_id) INNER JOIN courses USING(course_id) GROUP BY course;
- Add
echo "$($PSQL "SELECT course FROM students INNER JOIN majors_courses USING(major_id) INNER JOIN courses USING(course_id) GROUP BY course HAVING COUNT(student_id) = 1 ORDER BY course")"
to the bottom of thestudent_info.sh
file
This is the last step, you have done really well. Run the script one last time. 👋
- Run your
student_info.sh
script by executing it - Type
./student_info.sh
in the terminal and press enter - Make sure you are in the
project
folder first