What is the purpose of using SQL queries on DataFrames in Apache Spark?
SQL queries on DataFrames allow you to use familiar SQL syntax to analyze and manipulate data stored in Spark DataFrames, making it easier to perform complex data operations without writing complex code.
Click to reveal answer
beginner
How do you register a DataFrame as a temporary view in Spark to run SQL queries on it?
You use the createOrReplaceTempView method on the DataFrame with a name, for example: df.createOrReplaceTempView("my_table"). This lets you run SQL queries on 'my_table'.
Click to reveal answer
beginner
What Spark method do you use to run an SQL query on a registered DataFrame view?
You use spark.sql('your SQL query'). This returns a new DataFrame with the query results.
Click to reveal answer
beginner
Can you explain how to select specific columns from a DataFrame using SQL queries in Spark?
After registering the DataFrame as a temporary view, you write a SQL query like SELECT column1, column2 FROM view_name. This returns only those columns in the result DataFrame.
Click to reveal answer
intermediate
What is the difference between createOrReplaceTempView and createGlobalTempView in Spark?
createOrReplaceTempView creates a temporary view visible only in the current Spark session. createGlobalTempView creates a global temporary view accessible across different sessions but requires prefixing the view name with global_temp. when querying.
Click to reveal answer
Which method registers a DataFrame as a temporary view in Spark?
AcreateOrReplaceTempView
BregisterDataFrame
CcreateTempDataFrame
DregisterTempSQL
✗ Incorrect
The correct method to register a DataFrame as a temporary view is createOrReplaceTempView.
How do you run an SQL query on a registered DataFrame view in Spark?
Adf.sql('SQL query')
Bspark.sql('SQL query')
Cspark.query('SQL query')
Ddf.querySQL('SQL query')
✗ Incorrect
You use spark.sql('SQL query') to run SQL queries on registered views.
What does the SQL query SELECT * FROM my_table do on a DataFrame view?
ACreates a new table named my_table
BDeletes all data from the view
CUpdates all rows in the view
DSelects all columns and rows from the view
✗ Incorrect
SELECT * returns all columns and rows from the specified view.
Which prefix is required to query a global temporary view in Spark?
Aglobal.
Btemp_global.
Cglobal_temp.
Dtemp.
✗ Incorrect
Global temporary views require the prefix global_temp. when querying.
What type of object does spark.sql() return after running a query?
ADataFrame
BRDD
CList
DDictionary
✗ Incorrect
spark.sql() returns a DataFrame containing the query results.
Explain how to run an SQL query on a Spark DataFrame step-by-step.
Think about how SQL queries need a table name and how Spark lets you create that from a DataFrame.
You got /3 concepts.
Describe the difference between temporary and global temporary views in Spark.
Consider visibility and scope of views in Spark sessions.
You got /3 concepts.
Practice
(1/5)
1. What does the createOrReplaceTempView method do in Apache Spark?
easy
A. It registers a DataFrame as a temporary SQL table for querying.
B. It permanently saves the DataFrame to disk as a table.
C. It converts a SQL query into a DataFrame.
D. It deletes a temporary view from Spark.
Solution
Step 1: Understand the purpose of createOrReplaceTempView
This method registers a DataFrame as a temporary table in Spark's SQL context.
Step 2: Recognize its use in SQL queries
Once registered, you can run SQL queries on the DataFrame using spark.sql().
Final Answer:
It registers a DataFrame as a temporary SQL table for querying. -> Option A
Quick Check:
Temporary view = SQL table for queries [OK]
Hint: Temp view means temporary SQL table for queries [OK]
Common Mistakes:
Thinking it saves data permanently
Confusing it with running SQL queries
Assuming it deletes tables
2. Which of the following is the correct syntax to run a SQL query on a DataFrame registered as a temp view named people?
easy
A. spark.execute("SELECT * FROM people")
B. spark.query("SELECT * FROM people")
C. spark.sql("SELECT * FROM people")
D. spark.runSQL("SELECT * FROM people")
Solution
Step 1: Identify the correct method to run SQL queries
The method to run SQL queries on Spark is spark.sql().
Step 2: Check the syntax for querying the temp view
The query string should be passed inside spark.sql() as shown in spark.sql("SELECT * FROM people").
Final Answer:
spark.sql("SELECT * FROM people") -> Option C
Quick Check:
Use spark.sql() to run SQL queries [OK]
Hint: Use spark.sql() to run SQL queries on temp views [OK]
Common Mistakes:
Using incorrect method names like spark.query
Forgetting to use quotes around SQL query
Confusing method names with other APIs
3. Given the following code, what will be the output of the SQL query?
data = [(1, "Alice", 29), (2, "Bob", 31), (3, "Cathy", 25)]
columns = ["id", "name", "age"]
df = spark.createDataFrame(data, columns)
df.createOrReplaceTempView("people")
result = spark.sql("SELECT name FROM people WHERE age > 28 ORDER BY age DESC")
result.show()
medium
A. [["Bob"], ["Cathy"]]
B. [["Alice"], ["Bob"]]
C. [["Cathy"]]
D. [["Bob"], ["Alice"]]
Solution
Step 1: Analyze the data and filter condition
Rows with age > 28 are Alice (29) and Bob (31).
Step 2: Apply ORDER BY age DESC
Ordering by age descending gives Bob (31) first, then Alice (29).
Final Answer:
[["Bob"], ["Alice"]] -> Option D
Quick Check:
Filter age > 28 and order desc = Bob, Alice [OK]
Hint: Filter then order by age descending to get correct order [OK]
Common Mistakes:
Mixing ascending and descending order
Including rows with age <= 28
Confusing column names in SELECT
4. Identify the error in the following code snippet:
df.createOrReplaceTempView("employees")
result = spark.sql("SELECT * FROM employee WHERE salary > 50000")
result.show()
medium
A. The DataFrame was not created before creating temp view.
B. The temp view name is 'employees' but query uses 'employee'.
C. The SQL query syntax is incorrect.
D. The show() method cannot be used on SQL query results.
Solution
Step 1: Check temp view name and SQL query table name
The temp view is named "employees" but the query uses "employee" (missing 's').
Step 2: Understand impact of name mismatch
This causes a runtime error because the table "employee" does not exist.
Final Answer:
The temp view name is 'employees' but query uses 'employee'. -> Option B
Quick Check:
Temp view and query table names must match exactly [OK]
Hint: Check temp view and query table names match exactly [OK]
Common Mistakes:
Assuming SQL syntax error instead of name mismatch
Thinking show() can't be used on results
Ignoring case sensitivity in table names
5. You have a DataFrame sales with columns product, region, and amount. You want to find the total sales amount per region but only for regions where total sales exceed 1000. Which SQL query on the temp view sales will achieve this?
hard
A. SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region HAVING total_sales > 1000
B. SELECT region, SUM(amount) AS total_sales FROM sales WHERE total_sales > 1000 GROUP BY region
C. SELECT region, SUM(amount) FROM sales GROUP BY region WHERE SUM(amount) > 1000
D. SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region WHERE total_sales > 1000
Solution
Step 1: Understand filtering on aggregated data
Filtering on aggregated results requires the HAVING clause, not WHERE.
Step 2: Check correct use of HAVING with alias
Using HAVING total_sales > 1000 after GROUP BY is correct as in SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region HAVING total_sales > 1000.
Final Answer:
SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region HAVING total_sales > 1000 -> Option A
Quick Check:
Use HAVING to filter grouped aggregates [OK]
Hint: Use HAVING, not WHERE, to filter grouped results [OK]
Common Mistakes:
Using WHERE instead of HAVING for aggregates
Placing WHERE after GROUP BY
Not aliasing SUM(amount) before HAVING
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.