Hello!
I am doing the ‘Relational Database’ certification couse and I am on the module name:
Learn SQL by Building a Student Database: Part 2.
The challenge is:
2310. Add echo query result
2310.1
In your script, add the command to print what the sentence is asking for.
HINTS
- 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
the code in my file: student_info.sh is:
#!/bin/bash
#Info about my computer science students from students database
echo -e “\n~~ My Computer Science Students ~~\n”
PSQL=“psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c”
echo -e “\nFirst name, last name, and GPA of students with a 4.0 GPA:”
echo “$($PSQL “SELECT first_name, last_name, gpa FROM students WHERE gpa = 4.0”)”
echo -e “\nAll course names whose first letter is before ‘D’ in the alphabet:”
echo “$($PSQL “SELECT course FROM courses WHERE course < ‘D’”)”
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:”
echo “$($PSQL “SELECT first_name, last_name, gpa FROM students WHERE last_name >= ‘R’ AND (gpa > 3.8 OR gpa < 2.0)”)”
echo -e “\nLast name of students whose last name contains a case insensitive ‘sa’ or have an ‘r’ as the second to last letter:”
echo “$($PSQL “SELECT last_name FROM students WHERE last_name ILIKE ‘%sa%’ OR last_name ILIKE ‘%r_’”)”
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:”
echo “$($PSQL “SELECT first_name, last_name, gpa FROM students WHERE major_id IS NULL AND (first_name LIKE ‘D%’ OR gpa > 3.0)”)”
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’:”
echo “$($PSQL “select course from courses where course like ‘_e%’ or course like ‘%s’ order by course desc limit 5”)”
echo -e “\nAverage GPA of all students rounded to two decimal places:”
echo “$($PSQL “select round(avg(gpa), 2) from students”)”
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:”
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”)”
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’:”
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”)”
The error which i a getting is:
Your script should print the results of the suggested query correctly
- The script is not generating the output as it should. when i am executing the file in the bash terminal all the the command except the last echo command is executing perfectlly.
I tried to seperatelly perform the query in my PSQL terminal and I am getting the required output. Thus the query is correct and there is not problem in the database as well.