Querying JSON Data in SQL: Semi-Structured Data Meets Relational Power
JSON (JavaScript Object Notation) is the dominant format for semi-structured data in modern
applications. Relational databases now support JSON natively, letting you store flexible,
nested data alongside structured tables — and query it with SQL. This guide covers JSON
functions, JSON_TABLE, JSONPath, JSON vs relational, JSONB, and the 25 interview questions
that actually come up — with deep, descriptive theory aligned to the topic.
🎯 Key Takeaways
JSON is a lightweight, semi-structured data format
JSON in SQL combines flexibility with relational power
JSON_TABLE converts JSON to relational rows
JSONPath navigates JSON structures
JSONB is PostgreSQL's indexed JSON format
JSON vs relational is a key design decision
25 Interview QuestionsBeginner to AdvancedOracle, MySQL, PostgreSQL, SQL ServerUpdated Dec 3, 2026
KW
Kailas Warade
✓ Verified Author
Data Engineer · Oracle Certified · SQL & PL/SQL
Kailas works with SQL, PL/SQL, Python, and PySpark to build data pipelines.
He writes the SQL interview guides at Sankalan Data Tech, focused on
practical, real-world answers.
🏆 Oracle Certified💼 Data Engineer📝 SQL & PL/SQL Guides📍 Pune, India
JSON (JavaScript Object Notation) is a lightweight, text-based data format for storing and
exchanging structured data. It uses key-value pairs and arrays.
JSON is human-readable, easy to parse, and language-independent. It has become the
standard format for APIs, configuration files, and semi-structured data.
JSON supports a few basic types: string, number, boolean, null, object, and array. This
simplicity is both its strength (easy to use) and its limitation (no rich type system).
JSON is self-describing — the structure is embedded in the data itself. This makes it
flexible but also means the database cannot enforce a schema unless you add one.
Why JSON in Relational Databases
JSON is stored in relational databases for flexibility — schema-less data, nested
structures, and rapid prototyping — while still leveraging SQL querying and ACID
guarantees.
The key insight is that not all data fits neatly into rows and columns. Some data is
naturally nested or has varying structure. JSON handles this gracefully.
Storing JSON in a relational database gives you the best of both worlds: the flexibility
of JSON with the querying power, transactions, and tooling of SQL.
Common use cases include user preferences, product attributes, API responses, and
configuration data.
JSON vs Relational Data
JSON is schema-flexible and nested. Relational data is schema-rigid and flat (rows and
columns).
Aspect
JSON
Relational
Schema
Flexible, self-describing
Rigid, predefined
Structure
Nested, hierarchical
Flat, tabular
Best for
Semi-structured data
Structured data
Querying
JSONPath, JSON functions
SQL, joins
The choice depends on the data. Use JSON for flexible, nested, or rapidly changing data.
Use relational for stable, well-defined, highly relational data.
Many modern applications use both — relational for core entities, JSON for flexible
attributes.
JSON Functions Across Databases
Each database has its own JSON functions, but the concepts are similar.
-- Oracle: JSON_VALUE, JSON_QUERY, JSON_TABLE
SELECT JSON_VALUE(data, '$.name') FROM users;
-- MySQL: JSON_EXTRACT, ->
SELECT data->'$.name' FROM users;
-- PostgreSQL: ->, ->>
SELECT data->>'name' FROM users;
-- SQL Server: JSON_VALUE, JSON_QUERY
SELECT JSON_VALUE(data, '$.name') FROM users;
The functions differ in syntax but serve the same purpose: extracting values from JSON.
JSON_VALUE extracts a scalar value. JSON_QUERY extracts an object or array. This
distinction is important in Oracle and SQL Server.
JSON_TABLE in Depth
JSON_TABLE is an Oracle function that converts JSON data into relational rows and columns,
allowing you to query JSON as if it were a table.
SELECT *
FROM JSON_TABLE(
'{"employees": [{"name": "Alice", "salary": 50000}, {"name": "Bob", "salary": 60000}]}',
'$.employees[*]'
COLUMNS (
name VARCHAR2(100) PATH '$.name',
salary NUMBER PATH '$.salary'
)
);
This converts a JSON array of employees into relational rows with name and salary columns.
You can then use standard SQL (WHERE, JOIN, GROUP BY) on the result.
JSON_TABLE is powerful because it bridges the gap between JSON and relational. It lets you
apply full SQL power to JSON data.
JSON_TABLE is Oracle-specific. Other databases have similar concepts but different syntax.
JSONPath in Depth
JSONPath is a query language for JSON, similar to XPath for XML. It uses dot notation and
brackets to navigate JSON structures.
-- JSONPath examples
$.name -- root object, name key
$.address.city -- nested object
$.skills[0] -- first array element
$.employees[*].name -- all names in employees array
JSONPath uses $ for root, . for child, [] for array index or wildcard, and * for all.
JSONPath is the standard way to navigate JSON in SQL functions. Mastering it is essential
for working with JSON in any database.
JSONPath is similar to XPath for XML, but simpler and more intuitive.
JSONB in PostgreSQL
JSONB is a binary format for JSON in PostgreSQL, optimized for indexing and querying.
-- JSONB column
CREATE TABLE users (
id INT PRIMARY KEY,
data JSONB
);
-- Query JSONB
SELECT data->>'name' FROM users;
-- Index JSONB
CREATE INDEX idx_data ON users USING gin(data);
JSONB stores parsed binary, enabling indexing and faster queries but slower writes.
JSONB is the preferred format in PostgreSQL for read-heavy workloads. Plain JSON is better
for write-heavy workloads where the exact text must be preserved.
JSONB supports GIN indexes, which enable fast containment queries (@> operator).
25 Querying JSON Data in SQL Interview Questions and Answers
These are the JSON in SQL questions that come up most often in data engineering and
backend interviews. Every answer stands on its own — read them in any order, or work
through them in sequence to build a complete understanding of JSON in SQL, JSON functions,
JSON_TABLE, JSONPath, and JSONB.
/
🔍
No matching questions found
Try a keyword like JSON_TABLE, JSONPath, JSONB, or JSON functions.
JSON (JavaScript Object Notation) is a lightweight, text-based data format for
storing and exchanging structured data. It uses key-value pairs and arrays.
JSON is human-readable, easy to parse, and language-independent. It has become the
standard format for APIs and semi-structured data.
JSON supports a few basic types: string, number, boolean, null, object, and array.
JSON is stored in relational databases for flexibility — schema-less data, nested
structures, and rapid prototyping — while still leveraging SQL querying and ACID
guarantees.
-- JSON column in a relational table
CREATE TABLE users (
id INT PRIMARY KEY,
data JSON
);
The key insight is that not all data fits neatly into rows and columns. JSON handles
nested or varying structure gracefully.
This gives you the best of both worlds: JSON flexibility with SQL power.
JSON_TABLE is an Oracle function that converts JSON data into relational rows and
columns.
SELECT *
FROM JSON_TABLE(
'{"employees": [{"name": "Alice", "salary": 50000}]}',
'$.employees[*]'
COLUMNS (
name VARCHAR2(100) PATH '$.name',
salary NUMBER PATH '$.salary'
)
);
This converts JSON to relational rows, allowing standard SQL (WHERE, JOIN, GROUP BY)
on the result.
JSON_TABLE bridges the gap between JSON and relational.
JSONPath is a query language for JSON, similar to XPath for XML. It uses dot
notation and brackets to navigate JSON structures.
$.name -- root, name key
$.address.city -- nested
$.skills[0] -- first array element
$.employees[*].name -- all names
JSONPath uses $ for root, . for child, [] for array index or wildcard, and * for
all.
JSONPath is the standard way to navigate JSON in SQL functions.
JSON is lighter-weight, easier to parse, and more common in modern APIs. XML is more
verbose but supports namespaces and attributes.
Aspect
JSON
XML
Verbosity
Lightweight
Verbose
Parsing
Easy
More complex
Modern usage
Dominant in APIs
Legacy, enterprise
JSON has largely replaced XML in modern web APIs.
JSON is schema-flexible and nested. Relational data is schema-rigid and flat.
-- JSON: nested, flexible
{"user": {"name": "Alice", "address": {"city": "Pune"}}}
-- Relational: flat, rigid
| name | city |
| Alice | Pune |
Use JSON for flexible, nested data. Use relational for stable, well-defined data.
MySQL uses JSON_EXTRACT() and the -> operator to query JSON columns.
SELECT data->'$.name' FROM users;
SELECT JSON_EXTRACT(data, '$.name') FROM users;
Both approaches extract the value at the JSONPath $.name.
MySQL also supports ->> which unquotes the result.
PostgreSQL uses -> and ->> operators to query JSON/JSONB columns.
SELECT data->'name' FROM users; -- returns JSON
SELECT data->>'name' FROM users; -- returns text
-> returns JSON, ->> returns text. This distinction matters for further processing.
SQL Server uses JSON_VALUE() and JSON_QUERY() functions to query JSON columns.
SELECT JSON_VALUE(data, '$.name') FROM users;
SELECT JSON_QUERY(data, '$.address') FROM users;
JSON_VALUE extracts a scalar. JSON_QUERY extracts an object or array.
JSONB is a binary format for JSON in PostgreSQL, optimized for indexing and
querying.
CREATE TABLE users (
id INT PRIMARY KEY,
data JSONB
);
CREATE INDEX idx_data ON users USING gin(data);
JSONB stores parsed binary, enabling indexing and faster queries.
JSONB is preferred for read-heavy workloads in PostgreSQL.
JSON stores text as-is. JSONB stores parsed binary, enabling indexing and faster
queries but slower writes.
Aspect
JSON
JSONB
Storage
Text
Binary
Indexing
Limited
Full (GIN)
Read performance
Slower
Faster
Write performance
Faster
Slower
Use JSONB for read-heavy workloads. Use JSON for write-heavy workloads.
A JSON array is an ordered list of values, enclosed in square brackets.
Nested objects are accessed with chained dot notation: $.user.address.city.
Nesting is what makes JSON powerful for hierarchical data.
JSON data can be indexed using functional indexes on extracted values, or using GIN
indexes in PostgreSQL for JSONB.
-- Functional index (Oracle)
CREATE INDEX idx_name ON users(JSON_VALUE(data, '$.name'));
-- GIN index (PostgreSQL JSONB)
CREATE INDEX idx_data ON users USING gin(data);
Indexing JSON enables fast lookups on JSON values, just like regular columns.
JSON_VALUE extracts a scalar value. JSON_QUERY extracts an object or array.
-- JSON_VALUE: scalar
SELECT JSON_VALUE(data, '$.name') FROM users;
-- JSON_QUERY: object or array
SELECT JSON_QUERY(data, '$.address') FROM users;
Using the wrong one causes an error. JSON_VALUE for scalars, JSON_QUERY for
objects/arrays.
Updating JSON data depends on the database. Most support JSON_MODIFY (SQL Server),
JSON_SET (MySQL), or jsonb_set (PostgreSQL).
-- MySQL
UPDATE users SET data = JSON_SET(data, '$.name', 'Bob') WHERE id = 1;
-- PostgreSQL
UPDATE users SET data = jsonb_set(data, '{name}', '"Bob"') WHERE id = 1;
JSON updates are more complex than relational updates because you must navigate the
JSON structure.
JSON is nested and self-describing. CSV is flat and requires external schema.
JSON is better for APIs and small data. Parquet is better for analytics and large
data.
JSON has a null literal, but it is different from SQL NULL.
{"name": null} -- JSON null
-- In SQL, this may or may not map to SQL NULL depending on the database
JSON null and SQL NULL are conceptually similar but technically different. Be careful
when converting between them.
JSON uses braces and quotes. YAML uses indentation and is more human-readable.
-- JSON
{"name": "Alice"}
-- YAML
name: Alice
YAML is better for configuration files. JSON is better for APIs and data exchange.
JSON is text-based and self-describing. Protocol Buffers is binary and
schema-first.
-- JSON: text, self-describing
{"name": "Alice"}
-- Protocol Buffers: binary, schema-first
(message defined in .proto file)
JSON is better for APIs and debugging. Protocol Buffers is better for
performance-critical, schema-stable systems.
JSON validation depends on the database. Most support IS JSON (Oracle), JSON_VALID
(MySQL), or jsonb_typeof (PostgreSQL).
-- Oracle
SELECT * FROM users WHERE data IS JSON;
-- MySQL
SELECT * FROM users WHERE JSON_VALID(data);
Validation ensures that the JSON column contains valid JSON before processing.
Well-designed JSON in SQL follows a few key principles.
Use JSON for flexible data — not for stable, relational data.
Use JSONB in PostgreSQL — for read-heavy workloads.
Index JSON values — for fast lookups.
Validate JSON — before processing.
Use JSON Schema — for structure enforcement.
Keep JSON small — large JSON documents hurt performance.
-- Well-designed JSON column
CREATE TABLE users (
id INT PRIMARY KEY,
data JSONB -- PostgreSQL, indexed
);
CREATE INDEX idx_data ON users USING gin(data);
These principles produce a JSON-in-SQL design that is flexible, fast, and reliable.
Mistakes I Keep Seeing in Interviews
These come up repeatedly. None of them are exotic, and every one of them is easy to avoid
once you know it is coming.
Mistake one: confusing JSON with relational data. They serve different
purposes and have different trade-offs.
Mistake two: not knowing JSONPath. This is the standard way to navigate
JSON in SQL functions.
Mistake three: confusing JSON and JSONB. Know the difference: text vs
binary, indexing, performance.
Mistake four: using JSON for everything. JSON is not a replacement for
relational data — it is a complement.
Mistake five: not indexing JSON. Without indexes, JSON queries are slow.
Mistake six: confusing JSON_VALUE and JSON_QUERY. They extract different
things: scalar vs object/array.
Follow-Up Questions Interviewers Ask
Getting the first answer right is only half the interview. What usually decides the
outcome is what happens next. Here is what to prepare for on the JSON in SQL topic.
After “What is JSON?” — expect
“What are JSON data types?”
“What is a JSON array?”
“What is a nested JSON object?”
“What is JSONPath?”
After “JSON in SQL” — expect
“How do you query JSON in MySQL/PostgreSQL/SQL Server?”
“What is JSON_TABLE?”
“What is JSONB?”
“How do you index JSON?”
After “JSON vs relational” — expect
“When would you use JSON?”
“When would you use relational?”
“What are the trade-offs?”
“Can you use both?”
After “JSONB” — expect
“What is the difference between JSON and JSONB?”
“When would you use JSONB?”
“What is a GIN index?”
“How does JSONB affect performance?”
For every question above, prepare a second-level answer. The candidates who pass are not
the ones who memorized the first answer — they are the ones who kept going for another
thirty seconds and said something useful about why it matters.
Quick Quiz
Five questions to check whether the topics above have landed.
1. Which Oracle function converts JSON to relational rows?
2. Which PostgreSQL format is optimized for indexing?
3. Which JSONPath expression accesses the first element of an array?
4. Which function extracts a scalar value from JSON in SQL Server?
5. What is the main advantage of JSON in a relational database?