In this video, learn how to handle comma-separated values stored in a single column and calculate their sum using SQL. We'll walk through creating a table with student marks, writing a query to split and sum the values, and displaying the results. This is a great tutorial for those looking to enhance their SQL skills with practical examples. Perfect for database administrators and developers alike!
--you have a table with students names in one column and their marks as comma separated value in other column.
you need to take sum of those marks and display the result.
-- Step 1: Create the table
CREATE TABLE Students_Marks (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
marks VARCHAR(100) -- Comma-separated values
);
-- Step 2: Insert sample data
INSERT INTO Students_Marks (student_id, student_name, marks) VALUES
(1, 'John Doe', '75,85,90'),
(2, 'Jane Smith', '88,92,79'),
(3, 'Robert Brown', '65,70,72'),
(4, 'Emily Davis', '80,82,85');
-- Query to calculate the sum of marks
select student_id,student_name,
sum(cast(value as INT)) as Total_Marks
from Students_Marks
CROSS APPLY STRING_SPLIT(marks,',')
group by student_id,student_name
SQL Interview Problem: Splitting Full Names into First, Middle, and Last Names
• SQL Interview Problem: Splitting Full Name...
Top SQL Interview Questions: RANK, DENSE_RANK, ROW_NUMBER Explained
• Top SQL Interview Questions: RANK, DENSE_R...
Top 3 Products by Sales & Employees by Salary | SQL Ranking Functions Explained
• Top 3 Products by Sales & Employees by Sal...
SQL Interview Challenge: Find Hidden Likes in Your Friends' Favorites!
• SQL Interview Challenge: Find Hidden Likes...
SQL Magic: Aggregating Marks & Delivery Dates with Advanced Queries!
• SQL Magic: Aggregating Marks & Delivery Da...
Top 20% vs Bottom 20% of Students in SQL: NTILE vs TOP PERCENT
• Top 20% vs Bottom 20% of Students in SQL: ...
SQL Tricks: Find the 3rd Lowest Salary in Each Department with Two Powerful Methods!
• SQL Tricks: Find the 3rd Lowest Salary in ...
SQL Tutorial: Find Employees Who Joined Before Their Managers
• SQL Tutorial: Find Employees Who Joined Be...
SQL Tutorial: Retrieve Specific Rows with OFFSET & ROW_NUMBER - Top 2 Methods Compared!
• SQL Tutorial: Retrieve Specific Rows with ...
How to Build a Location-Based Hierarchy in SQL | Recursive Query Tutorial
• How to Build a Location-Based Hierarchy in...
How to Count Weekends in Any Month Using SQL Server: A Step-by-Step Guide
• How to Count Weekends in Any Month Using S...
SQL Date Magic: Find & Format the Last Day of the Previous Month
• SQL Date Magic: Find & Format the Last Day...
How to Count Workdays in SQL: Two Effective Methods Explained!
• How to Count Workdays in SQL: Two Effectiv...
How to Find Employees Earning More Than Their Managers in SQL
• How to Find Employees Earning More Than Th...
SQL Join Techniques: Filtering Data with WHERE vs. ON Clause
• SQL Join Techniques: Filtering Data with W...
Retrieve Specific Rows in SQL: OFFSET-FETCH vs. ROW_NUMBER!
• Retrieve Specific Rows in SQL: OFFSET-FETC...
#SQLTutorial
#CommaSeparatedValues
#SUMinSQL
#SQLAggregation
#DatabaseTutorial
#SQLQueryExamples