Skip to main content

Assert Student Postgres Query Matches Expected Results

Validate if the student's Postgres query matches the expected query

Location of the snippet: databases/postgres/assert_student_postgres_query_matches_expected_results

This assertion validates whether a student's Postgres query matches the expected results when executed against a specified database. It is useful for checking if the student's query correctly retrieves the intended data from the database.

Device Type
On-device

Variables:

Variable NameVariable DescriptionTypeRequired?Default
correct_queryThe correct querystrYes
database_nameThe name of the database to connect tostrYes
conn_kwargsADVANCED. Leave empty. Should be a literal dictionary that will be parsed in the execution environmentdictNo

Examples:

1. Basic SELECT Query Match

This example checks if the student's Postgres query exactly matches a simple SELECT statement.

Scenario: Students need to retrieve all data from the employees table in the company_db database.

Task: Write a Postgres query to select all columns from the employees table in company_db database.

Solution:

SELECT * FROM employees;

Snippet for the assertion:

Variable NameValue
correct_querySELECT * FROM employees;
database_namecompany_db

2. Specific Columns and WHERE Clause Match and connection kwargs

This example validates a query that selects specific columns and includes a WHERE clause for filtering and includes connection kwargs.

Scenario: Students are asked to find the name and email of users whose age is greater than 25 in the users table within the website_db database.

Task: Write a Postgres query to get the name and email of users older than 25 from the users table. To login to the database, use the username admin and password securepassword.

Solution:

SELECT name, email FROM users WHERE age > 25;

Snippet for the assertion:

Variable NameValue
correct_querySELECT name, email FROM users WHERE age > 25;
database_namewebsite_db
conn_kwargs{"user": "admin", "password": "securepassword"}