Using Spark SQL: 5 DBX-ASD Questions with Full Explanations

Using Spark SQL is a recurring theme in the Databricks Associate Developer for Apache Spark (DBX-ASD) exam. It makes up about 20% of the exam. The 5 questions below come with a full explanation of why each option is right or wrong.

DBX-ASD Using Spark SQL: 5 practice questions

Databricks Associate Developer for Apache Spark (DBX-ASD) · Using Spark SQL · 5 questions with answers and explanations

Question 1 of 5

A developer runs the code below. The data in df has five rows, and the filter keeps only two of them. `` df.createOrReplaceTempView("t") df = df.filter("x > 10") spark.sql("SELECT count(*) FROM t").show() `` Which statement about the count in the output is correct?

AIt counts two rows, because a view follows the latest value of the Python variable that was registered under its name
BIt counts two rows, because Spark pushes any later filter back into every view that was built from the same DataFrame object
CIt counts all five rows, because the view captured the original plan and reassigning the variable does not change it
DIt fails, because a view can no longer be queried once the DataFrame it was created from has been reassigned in Python
Show answer

✅ Answer: C

Difficulty: advanced · Topic: Spark SQL

DataFrames are immutable
Every transformation returns a new DataFrame with a new logical plan; the original is untouched. createOrReplaceTempView("t") registers the plan of the DataFrame at that moment.

What the example shows
After df = df.filter(...), the name df points to a new object. The view t still has the plan without the filter, so the count is 5. To get the filtered data into SQL, register the new DataFrame again under the same name.

Why each option works or fails
• ✗ A. Variable link: the view stores a plan, not the Python variable name. Rebinding df has no effect on the catalog.
• ✗ B. Pushdown into views: optimizer rules rewrite the plan of one query only. They never edit another registered plan.
• ✓ C. Immutable plan (correct): filter returns a new DataFrame. The name t still points to the plan that was registered before the reassignment.
• ✗ D. Dangling view: the old DataFrame object is only a handle. The plan lives in the catalog and stays valid.

Question 2 of 5

A DataFrame df has columns id (int) and msg (string). The developer runs df.write.text("/out/msgs") and the job fails with an AnalysisException. What is the cause and the fix?

AThe text sink supports only the first column, so Spark drops msg; the exception warns about data loss and mode("overwrite") suppresses it
BThe text sink writes every column separated by tabs, but the target directory must be created first with mkdir
CThe text sink takes one string column only; combine columns with concat_ws, or write a format such as CSV or JSON
DThe text sink needs the header option for multi-column data, so adding .option("header", True) fixes the write
Show answer

✅ Answer: C

Difficulty: applied · Topic: Spark SQL

The text writer is single column
The text source reads each line as one string column named value, and writes in the mirror image: the DataFrame must have exactly one column of string type, and each row becomes one line. A DataFrame with two columns is rejected before anything is written.

Ways to write several fields
• Build one string column (concat_ws(",", "id", "msg")) and write it as text. You then own quoting and escaping.
• Use csv or json writers, which handle several columns, headers and quoting.

Why each option works or fails
• ✗ A. Drop the second column: Spark does not silently truncate columns; the writer rejects the schema, and the save mode does not affect schema checks.
• ✗ B. Tab separation and mkdir: the text writer never joins columns with tabs, and Spark creates output directories itself, so the missing folder is not the problem.
• ✓ C. Single string column only (correct): the text writer cannot represent two columns, so the analysis fails; concatenating into one string column or choosing CSV/JSON solves it.
• ✗ D. header option: header is a CSV option. The text source has no header concept.

Question 3 of 5

A report tool needs CSV files with a header row, a semicolon separator and the text NA for NULL values. Which write call delivers this? df.write.option("header", True).option("sep", ";") .option("nullValue", "NA").csv("/out/report")

AThe call shown, because header, sep and nullValue are CSV options for writing.
BThe same call with .option("headers", True), because the header option name is plural.
CThe same call with .option("separator", ";"), because sep is only an alias used for reading.
DThe same call with .option("naValue", "NA"), because that is how nulls are written.
Show answer

✅ Answer: A

Difficulty: applied

CSV write options
The CSV source documents options for reading and writing.
• header: write a header line with the column names.
• sep (alias delimiter): the field separator, one or more characters. It applies to reading and writing.
• nullValue: the string that stands for a null. It applies to reading and writing.
Spark does not reject option names it does not know. A misspelled name such as separator or headers is silently ignored, and the write succeeds with default behavior (comma separator, no header).

Why each option works or fails
• ✓ A. (correct): header, sep and nullValue are the documented CSV option names, and all three apply on write, so the call produces the requested format.
• ✗ B. headers: the plural is not an option and would be ignored, so the file would have no header row.
• ✗ C. separator: that name does not exist and would be ignored, giving comma-separated output. The documented name sep works on writes too.
• ✗ D. naValue: it is not a CSV option. The documented one is nullValue.

Question 4 of 5

A table of 50 million devices is queried by device_id, and the team also joins it on device_id. A developer proposes partitionBy("device_id") when saving it as a table. Which assessment is correct?

AIt is the best choice, because one directory per device makes a device lookup read exactly one small file
BIt creates millions of tiny directories and files; bucketBy on device_id gives a fixed layout for a high-cardinality key
CIt hashes devices into 200 partitions on write, so the number of directories stays bounded by spark.sql.shuffle.partitions
DIt fails at write time, because partitionBy requires a string column and device_id is numeric
Show answer

✅ Answer: B

Difficulty: advanced · Topic: Spark SQL

Partitioning versus bucketing
• partitionBy(col) writes one directory per distinct value (device_id=123/). It gives partition pruning for filters on that column, but the directory count equals the number of distinct values. The guide notes it "has limited applicability to columns with high cardinality".
• bucketBy(n, col) hashes the column into a fixed number n of buckets. It "can be used when the number of unique values is unbounded", and helps joins and aggregations on that column.

Rule of thumb
Partition on low-cardinality columns that queries filter on (date, country). Bucket on high-cardinality keys used for joins or lookups (user id, device id).

Why each option works or fails
• ✗ A. One file per device: each directory holds at least one small file, but the cost of listing and tracking millions of them outweighs the pruning benefit.
• ✓ B. Too many directories (correct): partitionBy on 50 million values makes millions of small directories and files, which overloads listing and the metastore; bucketBy gives a fixed layout for a column with unbounded values.
• ✗ C. 200 partitions: the shuffle partition setting concerns shuffles, not the output directory layout. partitionBy writes a directory per value.
• ✗ D. String columns required: numeric, date and timestamp columns can be partition columns, and automatic type inference exists for them on read.

Question 5 of 5

A DataFrame with 3 partitions holds rows for 100 different countries mixed in every partition. The code df.write.partitionBy("country").parquet(out) produces about 300 small files. The team wants one file per country, and the write must stay parallel instead of funneling all data through one task. What should the code do before the write?

AIncrease spark.sql.files.maxPartitionBytes, because it merges the output files of a write.
BAdd .mode("overwrite"), because overwrite compacts files that belong to the same partition folder.
CCall df.coalesce(1) so that all rows are written by one task, which gives one file per country folder.
DCall df.repartition("country") so that all rows of a country sit in one partition before the write.
Show answer

✅ Answer: D

Difficulty: advanced

Why partitionBy can create many files
Every write task creates one file per distinct partition value that occurs in its own partition. With 3 tasks that each see 100 countries, the result is up to 3 x 100 = 300 files.
• repartition("country") shuffles all rows of a country into the same task, so each country folder gets one file (a task may serve several countries, but each folder still gets one file from it).
• coalesce(1) also gives one file per country, but a single task writes everything, which removes parallelism.
• Output size per file can be capped with the maxRecordsPerFile write option or spark.sql.files.maxRecordsPerFile.

Why each option works or fails
• ✗ A. maxPartitionBytes: it controls how files are split on reads. It has no effect on how many files a write produces.
• ✗ B. overwrite: the save mode decides what happens to existing data. It does not compact files within the new write.
• ✗ C. coalesce(1): it does produce one file per country, but it forces one task to write the whole dataset, which is slow for a large DataFrame. It is not the intended fix.
• ✓ D. (correct): Repartitioning by the same column first puts each country's rows into a single task, so each country folder receives one file.

🎯 Key ideas from this set

• A view captures the plan at registration; reassigning the variable does not update it.
• write.text needs exactly one string column; join columns into one or use csv/json.
• Use the documented CSV option names; misspelled ones are ignored without an error.
• Partition by low-cardinality filter columns; bucket by high-cardinality key columns.
• Repartition by the partitionBy column to get one file per partition value.

📊 Set profile

Exam: DBX-ASD · 45 questions · 90 minutes
This set: 3 advanced, 2 applied · 2 troubleshooting, 1 comparison, 1 concept check, 1 trade-off
Topics: Spark SQL

🔗 Keep practicing on this blog

• Structured Streaming: 5 DBX-ASD Questions with Full Explanations
• Can You Answer These 5 DBX-ASD Questions on Using Spark Connect to Deploy Applications?
• 5 Questions to Test Your Using Pandas API on Spark Knowledge (DBX-ASD)
• All DBX-ASD practice sets
• Every exam on this blog

📚 More DBX-ASD practice

More DBX-ASD questions: https://hyunhp.github.io/cert-bank/#exam=DBX-ASD
Study guide: https://hyunhp.github.io/cert-bank/study.html#exam=DBX-ASD

These are original practice questions, not real exam questions, and cert-bank is not affiliated with any certification vendor.

Original practice questions written for this blog, not exam dumps.

Comments