Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling json data in sql #1864

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ Thanks to these amazing people who have contributed to the **Algo** project:
<sub><b>Kundan Rajoria</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/PavanTeja2005">
<img src="https://avatars.githubusercontent.com/u/98730339?v=4" width="100;" alt="PavanTeja2005"/>
<br />
<sub><b>PavanTeja2005</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/tanishqkolhatkar93">
<img src="https://avatars.githubusercontent.com/u/91214235?v=4" width="100;" alt="tanishqkolhatkar93"/>
Expand All @@ -463,13 +470,6 @@ Thanks to these amazing people who have contributed to the **Algo** project:
<br />
<sub><b>riddhi</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/PavanTeja2005">
<img src="https://avatars.githubusercontent.com/u/98730339?v=4" width="100;" alt="PavanTeja2005"/>
<br />
<sub><b>PavanTeja2005</b></sub>
</a>
</td>
</tr>
<tr>
Expand All @@ -481,17 +481,17 @@ Thanks to these amazing people who have contributed to the **Algo** project:
</a>
</td>
<td align="center">
<a href="https://github.com/Mansi07sharma">
<img src="https://avatars.githubusercontent.com/u/142892607?v=4" width="100;" alt="Mansi07sharma"/>
<a href="https://github.com/jainaryan04">
<img src="https://avatars.githubusercontent.com/u/138214350?v=4" width="100;" alt="jainaryan04"/>
<br />
<sub><b>Mansi07sharma</b></sub>
<sub><b>Aryan Ramesh Jain</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/jainaryan04">
<img src="https://avatars.githubusercontent.com/u/138214350?v=4" width="100;" alt="jainaryan04"/>
<a href="https://github.com/Mansi07sharma">
<img src="https://avatars.githubusercontent.com/u/142892607?v=4" width="100;" alt="Mansi07sharma"/>
<br />
<sub><b>Aryan Ramesh Jain</b></sub>
<sub><b>Mansi07sharma</b></sub>
</a>
</td>
<td align="center">
Expand Down
87 changes: 87 additions & 0 deletions docs/languages/SQL/sql-25.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
id: SQL-JSON-Data
jainaryan04 marked this conversation as resolved.
Show resolved Hide resolved
sidebar_position: 25
title: "JSON Data in SQL"
sidebar_label: "JSON Data in SQL"
description: "Handling JSON Data Within SQL."
tags: [sql, dbms, database, json, data, backend]
---

## Introduction
With the growing use of JSON (JavaScript Object Notation) for data exchange, many modern databases have introduced native support for JSON data types. This enables efficient querying and manipulation of JSON data directly within SQL.

## JSON Data Type
Many SQL databases, including MySQL, PostgreSQL, and SQL Server, support a `JSON` data type, allowing structured data to be stored in a single column.

### Benefits:
- **Structured Storage**: Store hierarchical data.
- **Efficient Access**: Query and manipulate JSON data using SQL.
- **Flexibility**: Schema-less data storage.

## Storing JSON in SQL
You can store JSON data in a `JSON` or `TEXT` column depending on the database.

### Example:
```sql
CREATE TABLE users (
id INT PRIMARY KEY,
details JSON
);

INSERT INTO users (id, details) VALUES
(1, '{"name": "Alice", "age": 30, "email": "alice@example.com"}');
```

## Querying JSON Data
SQL provides functions to query and extract values from JSON data.

### MySQL:
```sql
SELECT details->'$.name' AS name FROM users;
```

### PostgreSQL:
```sql
SELECT details->>'name' AS name FROM users;
```

### SQL Server:
```sql
SELECT JSON_VALUE(details, '$.name') AS name FROM users;
```

## Modifying JSON Data
Use JSON functions to update, add, or remove JSON elements.

### MySQL Example:
```sql
UPDATE users SET details = JSON_SET(details, '$.age', 31) WHERE id = 1;
```

### PostgreSQL Example:
```sql
UPDATE users SET details = jsonb_set(details, '{age}', '31', false) WHERE id = 1;
```

## Indexing JSON Data
JSON fields can be indexed to improve query performance.

### MySQL:
```sql
ALTER TABLE users ADD INDEX idx_name ((details->'$.name'));
```

### PostgreSQL:
```sql
CREATE INDEX idx_name ON users ((details->>'name'));
```

## Use Cases
- **Flexible Data Models**: Store varying data structures.
- **API Integration**: Easily handle JSON responses.
- **Data Analysis**: Aggregate and filter nested data.

## Conclusion
Handling JSON data within SQL combines the flexibility of JSON with the robustness of SQL databases. By leveraging native JSON support, developers can efficiently store, query, and manipulate JSON data, optimizing database operations for modern applications.

This file provides a comprehensive guide on handling JSON data within SQL, covering essential operations and examples for different databases.
Loading