Skip to content

Commit

Permalink
Merge pull request #1864 from jainaryan04/main
Browse files Browse the repository at this point in the history
handling json data in sql
  • Loading branch information
ajay-dhangar authored Nov 7, 2024
2 parents 82d7249 + cad81f0 commit e8642a6
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 60 deletions.
120 changes: 60 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,19 @@ Thanks to these amazing people who have contributed to the **Algo** project:
</a>
</td>
<td align="center">
<a href="https://github.com/PrAyAg9">
<img src="https://avatars.githubusercontent.com/u/114804510?v=4" width="100;" alt="PrAyAg9"/>
<a href="https://github.com/Shariq2003">
<img src="https://avatars.githubusercontent.com/u/109452033?v=4" width="100;" alt="Shariq2003"/>
<br />
<sub><b>Prayag Thakur</b></sub>
<sub><b>Shariq</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/AE-Hertz">
<img src="https://avatars.githubusercontent.com/u/93651229?v=4" width="100;" alt="AE-Hertz"/>
<a href="https://github.com/MithanshuHedau">
<img src="https://avatars.githubusercontent.com/u/144697427?v=4" width="100;" alt="MithanshuHedau"/>
<br />
<sub><b>Abhinandan</b></sub>
<sub><b>Mithanshu Hedau</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -356,17 +356,24 @@ Thanks to these amazing people who have contributed to the **Algo** project:
</a>
</td>
<td align="center">
<a href="https://github.com/MithanshuHedau">
<img src="https://avatars.githubusercontent.com/u/144697427?v=4" width="100;" alt="MithanshuHedau"/>
<a href="https://github.com/AE-Hertz">
<img src="https://avatars.githubusercontent.com/u/93651229?v=4" width="100;" alt="AE-Hertz"/>
<br />
<sub><b>Mithanshu Hedau</b></sub>
<sub><b>Abhinandan</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Shariq2003">
<img src="https://avatars.githubusercontent.com/u/109452033?v=4" width="100;" alt="Shariq2003"/>
<a href="https://github.com/PrAyAg9">
<img src="https://avatars.githubusercontent.com/u/114804510?v=4" width="100;" alt="PrAyAg9"/>
<br />
<sub><b>Shariq</b></sub>
<sub><b>Prayag Thakur</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/shriyadindi">
<img src="https://avatars.githubusercontent.com/u/126611334?v=4" width="100;" alt="shriyadindi"/>
<br />
<sub><b>Shriya Dindi</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -376,29 +383,22 @@ Thanks to these amazing people who have contributed to the **Algo** project:
<sub><b>Mugundh J B</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/pratheekv39">
<img src="https://avatars.githubusercontent.com/u/165366548?v=4" width="100;" alt="pratheekv39"/>
<br />
<sub><b>V Pratheek </b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/monishkumardvs">
<img src="https://avatars.githubusercontent.com/u/125813798?v=4" width="100;" alt="monishkumardvs"/>
<br />
<sub><b>Dvs monish kumar</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/shriyadindi">
<img src="https://avatars.githubusercontent.com/u/126611334?v=4" width="100;" alt="shriyadindi"/>
<br />
<sub><b>Shriya Dindi</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/aditiverma-21">
<img src="https://avatars.githubusercontent.com/u/180853305?v=4" width="100;" alt="aditiverma-21"/>
Expand All @@ -407,40 +407,54 @@ Thanks to these amazing people who have contributed to the **Algo** project:
</a>
</td>
<td align="center">
<a href="https://github.com/Alpha1zln">
<img src="https://avatars.githubusercontent.com/u/98889077?v=4" width="100;" alt="Alpha1zln"/>
<a href="https://github.com/Subashree-selvaraj">
<img src="https://avatars.githubusercontent.com/u/132484553?v=4" width="100;" alt="Subashree-selvaraj"/>
<br />
<sub><b>SHREYAS YADUVANSHI</b></sub>
<sub><b>subashree</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Rashigera">
<img src="https://avatars.githubusercontent.com/u/20163397?v=4" width="100;" alt="Rashigera"/>
<a href="https://github.com/Abhishek2634">
<img src="https://avatars.githubusercontent.com/u/136872441?v=4" width="100;" alt="Abhishek2634"/>
<br />
<sub><b>Rashigera</b></sub>
<sub><b>Abhishek Farshwal</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Subashree-selvaraj">
<img src="https://avatars.githubusercontent.com/u/132484553?v=4" width="100;" alt="Subashree-selvaraj"/>
<a href="https://github.com/KashishJuneja101003">
<img src="https://avatars.githubusercontent.com/u/69794677?v=4" width="100;" alt="KashishJuneja101003"/>
<br />
<sub><b>subashree</b></sub>
<sub><b>Kashish Juneja</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Abhishek2634">
<img src="https://avatars.githubusercontent.com/u/136872441?v=4" width="100;" alt="Abhishek2634"/>
<a href="https://github.com/jainaryan04">
<img src="https://avatars.githubusercontent.com/u/138214350?v=4" width="100;" alt="jainaryan04"/>
<br />
<sub><b>Abhishek Farshwal</b></sub>
<sub><b>Aryan Ramesh Jain</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/KashishJuneja101003">
<img src="https://avatars.githubusercontent.com/u/69794677?v=4" width="100;" alt="KashishJuneja101003"/>
<a href="https://github.com/Alpha1zln">
<img src="https://avatars.githubusercontent.com/u/98889077?v=4" width="100;" alt="Alpha1zln"/>
<br />
<sub><b>Kashish Juneja</b></sub>
<sub><b>SHREYAS YADUVANSHI</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Rashigera">
<img src="https://avatars.githubusercontent.com/u/20163397?v=4" width="100;" alt="Rashigera"/>
<br />
<sub><b>Rashigera</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Riddhi12349">
<img src="https://avatars.githubusercontent.com/u/91315409?v=4" width="100;" alt="Riddhi12349"/>
<br />
<sub><b>riddhi</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -457,6 +471,8 @@ Thanks to these amazing people who have contributed to the **Algo** project:
<sub><b>Tatheer Fathima</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Mansi07sharma">
<img src="https://avatars.githubusercontent.com/u/142892607?v=4" width="100;" alt="Mansi07sharma"/>
Expand All @@ -465,14 +481,12 @@ Thanks to these amazing people who have contributed to the **Algo** project:
</a>
</td>
<td align="center">
<a href="https://github.com/Riddhi12349">
<img src="https://avatars.githubusercontent.com/u/91315409?v=4" width="100;" alt="Riddhi12349"/>
<a href="https://github.com/mehul-m-prajapati">
<img src="https://avatars.githubusercontent.com/u/7879392?v=4" width="100;" alt="mehul-m-prajapati"/>
<br />
<sub><b>riddhi</b></sub>
<sub><b>Mehul Prajapati</b></sub>
</a>
</td>
</tr>
<tr>
<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 @@ -487,13 +501,6 @@ Thanks to these amazing people who have contributed to the **Algo** project:
<sub><b>PavanTeja2005</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/mehul-m-prajapati">
<img src="https://avatars.githubusercontent.com/u/7879392?v=4" width="100;" alt="mehul-m-prajapati"/>
<br />
<sub><b>Mehul Prajapati</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/sarthaxtic">
<img src="https://avatars.githubusercontent.com/u/128150513?v=4" width="100;" alt="sarthaxtic"/>
Expand All @@ -502,27 +509,20 @@ Thanks to these amazing people who have contributed to the **Algo** project:
</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/shimmer12">
<img src="https://avatars.githubusercontent.com/u/92056170?v=4" width="100;" alt="shimmer12"/>
<br />
<sub><b>Aryan Ramesh Jain</b></sub>
<sub><b>Srishti Soni</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Asmi1108">
<img src="https://avatars.githubusercontent.com/u/174241668?v=4" width="100;" alt="Asmi1108"/>
<br />
<sub><b>Asmi</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/shimmer12">
<img src="https://avatars.githubusercontent.com/u/92056170?v=4" width="100;" alt="shimmer12"/>
<br />
<sub><b>Srishti Soni</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/khurshed07">
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
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.

0 comments on commit e8642a6

Please sign in to comment.