Understanding SQL Array and JSON Functions with Examples

Dive into the world of SQL Array and JSON functions with insightful examples. Enhance your data handling skills and level up your SQL expertise today!

Kaibarta Sa

1/4/20243 min read

men holding guns
men holding guns

Introduction

Structured Query Language (SQL) is a powerful tool for managing and manipulating data in relational databases. Over the years, SQL has evolved to include various functions and features that enhance its capabilities. Two such features are SQL array and JSON functions.

SQL Array Functions

Arrays in SQL are a collection of similar data types that are stored as a single entity. SQL provides several functions to work with arrays:

ARRAY_LENGTH

The ARRAY_LENGTH function returns the number of elements in an array.

SELECT ARRAY_LENGTH(array_column) AS length
FROM table_name;

ARRAY_APPEND

The ARRAY_APPEND function adds an element to the end of an array.

SELECT ARRAY_APPEND(array_column, element) AS new_array
FROM table_name;

ARRAY_REMOVE

The ARRAY_REMOVE function removes all occurrences of an element from an array.

SELECT ARRAY_REMOVE(array_column, element) AS new_array
FROM table_name;

ARRAY_SORT

The ARRAY_SORT function sorts an array in ascending order.

SELECT ARRAY_SORT(array_column) AS sorted_array
FROM table_name;

SQL JSON Functions

JSON (JavaScript Object Notation) is a lightweight data interchange format that is widely used for storing and exchanging data. SQL provides several functions to work with JSON:

JSON_ARRAY_LENGTH

The JSON_ARRAY_LENGTH function returns the number of elements in a JSON array.

SELECT JSON_ARRAY_LENGTH(json_column) AS length
FROM table_name;

JSON_EXTRACT

The JSON_EXTRACT function extracts a value from a JSON object based on a specified path.

SELECT JSON_EXTRACT(json_column, '$.key') AS value
FROM table_name;

JSON_ARRAY_APPEND

The JSON_ARRAY_APPEND function appends elements to a JSON array.

SELECT JSON_ARRAY_APPEND(json_column, '$', element) AS new_json
FROM table_name;

JSON_REMOVE

The JSON_REMOVE function removes a specified value from a JSON object or array.

SELECT JSON_REMOVE(json_column, '$.key') AS new_json
FROM table_name;

Example: SQL Array Functions

Let's consider a scenario where we have a table called "employees" with a column named "skills" that stores an array of skills for each employee.

+----+-------------------+
| ID |      Skills       |
+----+-------------------+
| 1  | [Java, SQL, HTML] |
| 2  | [Python, SQL]     |
| 3  | [JavaScript]      |
+----+-------------------+

To find the length of the skills array for each employee, we can use the ARRAY_LENGTH function:

SELECT ID, ARRAY_LENGTH(skills) AS num_skills
FROM employees;

To add a new skill "CSS" to the skills array for employee with ID 2, we can use the ARRAY_APPEND function:

SELECT ID, ARRAY_APPEND(skills, 'CSS') AS updated_skills
FROM employees
WHERE ID = 2;

To remove the skill "HTML" from the skills array for all employees, we can use the ARRAY_REMOVE function:

SELECT ID, ARRAY_REMOVE(skills, 'HTML') AS updated_skills
FROM employees;

To sort the skills array in ascending order for each employee, we can use the ARRAY_SORT function:

SELECT ID, ARRAY_SORT(skills) AS sorted_skills
FROM employees;

Example: SQL JSON Functions

Let's consider a scenario where we have a table called "products" with a column named "details" that stores product information in JSON format.

+----+---------------------------------------------------------+
| ID |                         Details                         |
+----+---------------------------------------------------------+
| 1  | {"name": "iPhone", "price": 999, "category": "Electronics"} |
| 2  | {"name": "Samsung TV", "price": 1499, "category": "Electronics"} |
| 3  | {"name": "Book", "price": 19.99, "category": "Books"} |
+----+---------------------------------------------------------+

To find the length of the details JSON array for each product, we can use the JSON_ARRAY_LENGTH function:

SELECT ID, JSON_ARRAY_LENGTH(details) AS num_details
FROM products;

To extract the value of the "name" key from the details JSON object for each product, we can use the JSON_EXTRACT function:

SELECT ID, JSON_EXTRACT(details, '$.name') AS product_name
FROM products;

To append a new key-value pair "rating": 4.5 to the details JSON object for product with ID 3, we can use the JSON_ARRAY_APPEND function:

SELECT ID, JSON_ARRAY_APPEND(details, '$', '{"rating": 4.5}') AS updated_details
FROM products
WHERE ID = 3;

To remove the "price" key from the details JSON object for all products, we can use the JSON_REMOVE function:

SELECT ID, JSON_REMOVE(details, '$.price') AS updated_details
FROM products;

Conclusion

SQL array and JSON functions provide powerful tools for working with arrays and JSON data in relational databases. By leveraging these functions, developers and data analysts can efficiently manipulate and extract information from complex data structures. Understanding how to use these functions can greatly enhance your SQL skills and enable you to work with a wide range of data formats.

Remember to practice and experiment with these functions to gain a deeper understanding of their capabilities. With the right knowledge and experience, you can leverage SQL array and JSON functions to unlock new insights and possibilities in your data analysis and application development tasks.