databricks Associate Developer for Apache Spark · Practice questions

Section 2: Using Spark SQL Practice Questions

Review 10 sample questions with their correct answers and explanations. Sign up free to continue with the full topic quiz.

Sign up free for the full quiz

Question 1

Which statement about persistent tables created with df.write.saveAsTable('sales') is correct?

  1. The table disappears when the SparkSession ends
  2. The table can only be queried with the DataFrame API, not SQL
  3. It is registered in the catalog and outlives the session — Correct
  4. The table is only visible to the current notebook

Correct answer

It is registered in the catalog and outlives the session

Explanation

saveAsTable writes data and registers a table in the metastore, unlike temporary views, which are session-scoped.

Question 2

Which statement about spark.table('sales') is correct?

  1. It returns a DataFrame for a catalog table or view — Correct
  2. It creates an empty table named sales
  3. It returns the table's schema as a string
  4. It reads a file named sales in the working directory

Correct answer

It returns a DataFrame for a catalog table or view

Explanation

spark.table (or spark.read.table) loads a catalog table or view as a DataFrame.

Question 3

A view must be accessible from different SparkSessions in the same application. Which method should be used, and how is the view referenced?

  1. createOrReplaceTempView('v'), referenced as v in any session
  2. createOrReplaceGlobalTempView('v'), read as global_temp.v — Correct
  3. createGlobalView('v'), referenced as default.v
  4. saveAsTable('v'), referenced as temp.v

Correct answer

createOrReplaceGlobalTempView('v'), read as global_temp.v

Explanation

Global temporary views live in the global_temp database and are shared across sessions of the same application until it ends.

Question 4

Which statement about querying CSV files directly with SELECT * FROM csv.`/path` is correct?

  1. Reader options cannot be set, so columns become _c0, _c1 strings — Correct
  2. It creates a permanent table named csv
  3. The header row is detected automatically and types are inferred
  4. It only works for files smaller than 1 MB

Correct answer

Reader options cannot be set, so columns become _c0, _c1 strings

Explanation

The direct-file syntax uses default reader options. For headers, delimiters or schemas, create a view or table with options, or use read_files or the DataFrame reader.

Question 5

Which SQL statement creates a table from a query and saves it as Parquet partitioned by country?

  1. CREATE VIEW sales_p USING parquet PARTITIONED BY (country) AS SELECT * FROM sales
  2. SAVE TABLE sales_p FROM sales USING parquet BY country
  3. CREATE TABLE sales_p USING parquet PARTITIONED BY (country) AS SELECT * FROM sales — Correct
  4. CREATE TABLE sales_p AS SELECT * FROM sales PARTITION BY country STORED AS parquet FORMAT

Correct answer

CREATE TABLE sales_p USING parquet PARTITIONED BY (country) AS SELECT * FROM sales

Explanation

CTAS with USING and PARTITIONED BY creates a partitioned table in the given format. Views do not store data.

Question 6

A user runs spark.sql('SELECT * FROM delta.`/mnt/data/orders`'). What does this do?

  1. Converts the Parquet files at that path to Delta
  2. It queries the Delta table at that path — Correct
  3. Creates a new Delta table named delta
  4. Fails because Delta tables must be registered first

Correct answer

It queries the Delta table at that path

Explanation

The format.`path` syntax works for Delta as well, reading the table at that path.

Question 7

A Python UDF must be callable from Spark SQL queries. Which code registers it?

  1. spark.catalog.createFunction('clean_name')
  2. spark.sql.register('clean_name', clean_name_fn)
  3. spark.udf.register('clean_name', clean_name_fn, StringType()) — Correct
  4. df.createOrReplaceTempView('clean_name')

Correct answer

spark.udf.register('clean_name', clean_name_fn, StringType())

Explanation

spark.udf.register makes the function available to SQL under the given name.

Question 8

Which statement about spark.sql() is correct?

  1. It returns results as a Python list
  2. It returns a DataFrame usable with the DataFrame API — Correct
  3. It executes the query immediately even without an action
  4. It can only query permanent tables

Correct answer

It returns a DataFrame usable with the DataFrame API

Explanation

spark.sql returns a DataFrame; like other transformations, SELECT queries execute when an action runs.

Question 9

Which code writes a DataFrame to a partitioned table in the catalog?

  1. df.write.bucketBy('country').saveAsTable('sales_by_country')
  2. df.write.partitionBy('country').saveAsTable('sales_by_country') — Correct
  3. df.partitionBy('country').saveAsTable('sales_by_country')
  4. df.write.saveAsTable('sales_by_country', partitionBy='country', mode='overwrite', format='parquet')

Correct answer

df.write.partitionBy('country').saveAsTable('sales_by_country')

Explanation

partitionBy on the DataFrameWriter is combined with saveAsTable to create a partitioned table.

Question 10

Given: df.write.mode('append').saveAsTable('t') executed twice with the same 100-row DataFrame. How many rows does table t contain if it was empty before?

  1. 0
  2. 200 — Correct
  3. The second write fails
  4. 100

Correct answer

200

Explanation

Append adds rows on each write, so the table has 200 rows.

Ready for the full quiz?

Create a free QuizAnalyst account to practice the complete question set and save your progress.

Sign up free for the full quiz