0
0
Apache Airflowdevops~20 mins

SqlOperator for database queries in Apache Airflow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SqlOperator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Airflow SqlOperator task?
Given the following Airflow DAG snippet, what will be the output in the Airflow logs when the task runs successfully?
Apache Airflow
from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator
from datetime import datetime

default_args = {'start_date': datetime(2024, 1, 1)}

dag = DAG('test_sql_operator', default_args=default_args, schedule_interval='@once')

run_query = PostgresOperator(
    task_id='run_query',
    postgres_conn_id='my_postgres',
    sql='SELECT COUNT(*) FROM users;',
    dag=dag
)
AThe task will succeed but no output will be shown in logs by default.
BThe task will fail because SqlOperator cannot run SELECT queries.
CThe task logs will show the count of rows in the users table as a result.
DThe task will raise a syntax error due to missing parameters.
Attempts:
2 left
💡 Hint
Think about what SqlOperator does with SELECT queries and how results are handled.
Configuration
intermediate
2:00remaining
Which configuration correctly runs a SQL script file using SqlOperator?
You want to run a SQL script stored in '/sql_scripts/create_tables.sql' using Airflow's SqlOperator. Which option correctly configures the operator?
APostgresOperator(task_id='create_tables', postgres_conn_id='my_postgres', sql=open('/sql_scripts/create_tables.sql').read(), dag=dag)
BPostgresOperator(task_id='create_tables', postgres_conn_id='my_postgres', sql='RUN /sql_scripts/create_tables.sql', dag=dag)
CPostgresOperator(task_id='create_tables', postgres_conn_id='my_postgres', sql='@/sql_scripts/create_tables.sql', dag=dag)
DPostgresOperator(task_id='create_tables', postgres_conn_id='my_postgres', sql='/sql_scripts/create_tables.sql', dag=dag)
Attempts:
2 left
💡 Hint
SqlOperator's sql parameter accepts a string containing the SQL commands.
Troubleshoot
advanced
2:00remaining
Why does this SqlOperator task fail with 'could not connect to server' error?
You have this task: PostgresOperator(task_id='check_data', postgres_conn_id='wrong_conn', sql='SELECT 1;', dag=dag) It fails with 'could not connect to server' error. What is the most likely cause?
AThe DAG start_date is in the future, so the task cannot connect.
BThe SQL query 'SELECT 1;' is invalid and causes connection failure.
CThe postgres_conn_id 'wrong_conn' is not defined or has wrong connection details in Airflow connections.
DThe PostgresOperator requires extra parameters to connect, missing here.
Attempts:
2 left
💡 Hint
Check the connection ID and its configuration in Airflow UI.
🔀 Workflow
advanced
2:00remaining
How to chain multiple SqlOperator tasks to run sequentially?
You have three SQL tasks: create_table, insert_data, and verify_data. How do you set them to run one after another in Airflow?
Apache Airflow
create_table = PostgresOperator(task_id='create_table', postgres_conn_id='my_postgres', sql='CREATE TABLE test(id INT);', dag=dag)
insert_data = PostgresOperator(task_id='insert_data', postgres_conn_id='my_postgres', sql='INSERT INTO test VALUES (1);', dag=dag)
verify_data = PostgresOperator(task_id='verify_data', postgres_conn_id='my_postgres', sql='SELECT COUNT(*) FROM test;', dag=dag)
Acreate_table | insert_data | verify_data
Bcreate_table & insert_data & verify_data
Ccreate_table + insert_data + verify_data
Dcreate_table >> insert_data >> verify_data
Attempts:
2 left
💡 Hint
Airflow uses bitshift operators to set task dependencies.
Best Practice
expert
2:00remaining
What is the best practice to handle sensitive database credentials with SqlOperator?
You want to run SQL queries securely in Airflow without exposing database passwords in DAG code. What is the best practice?
ASave credentials in a plain text file and read them in the DAG code.
BStore credentials in Airflow Connections and reference them by connection ID in SqlOperator.
CHardcode the username and password directly in the sql parameter string.
DUse environment variables inside the sql parameter string to pass credentials.
Attempts:
2 left
💡 Hint
Airflow has a built-in way to manage secrets and connections.