How to calculate percentage with a SQL statement

0 votes
Users and their grades are contained in a SQL Server table I have. Let's assume for the sake of simplicity that there are just 2 columns: name and grade. For example, a common row may read Name: "John Doe," Grade: "A."

I'm trying to find a single SQL statement that will return the percentages for every potential response. Is there a way to do this without specifying every possible response (e.g., an open text box where users might type "pass/fail," "none," etc.)

I want the following final results: A: 5%, B: 15%, C: 40%, etc.

Can someone please help m with this?
Sep 10, 2022 in Database by Kithuzzz
• 38,000 points
5,821 views

1 answer to this question.

0 votes

The following has passed my tests, and it is effective. Although gordyii's response was close, it erroneously multiplied by 100 and lacked some parenthesis.

Select Grade, (Count(Grade)* 100 / (Select Count(*) From MyTable)) as Score
From MyTable
Group By Grade

I hope this helps you.

To know more about SQL, It's recommended to join Microsoft SQL Training today.

answered Sep 12, 2022 by narikkadan
• 63,600 points

Related Questions In Database

0 votes
0 answers

How to DROP multiple columns with a single ALTER TABLE statement in SQL Server?

In one ALTER TABLE statement, I would ...READ MORE

Sep 2, 2022 in Database by Kithuzzz
• 38,000 points
751 views
0 votes
1 answer

How to drop all tables from a database with one SQL query?

Use the INFORMATION_SCHEMA.TABLES view to get the ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,020 points
8,982 views
0 votes
0 answers

How would I use excel to generate a large update sql statement?

I know there's a way to have ...READ MORE

Mar 30, 2022 in Database by Edureka
• 13,690 points
2,358 views
0 votes
0 answers

How to display employee names starting with a and then b in sql

I want to display the names of ...READ MORE

Aug 12, 2022 in Database by Kithuzzz
• 38,000 points
930 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,070 points
822 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4, 2022 in Database by Vaani
• 7,070 points
548 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,020 points
1,057 views
0 votes
1 answer

LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

At the top level there are mainly ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,020 points
1,993 views
0 votes
1 answer

Add a column with a default value to an existing table in SQL Server

Syntax: ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL|NOT ...READ MORE

answered Sep 12, 2022 in Database by narikkadan
• 63,600 points
2,502 views
0 votes
1 answer

How to add a Try/Catch to SQL Stored Procedure

See TRY...CATCH (Transact-SQL) CREATE PROCEDURE [dbo].[PL_GEN_PROVN_NO1] ...READ MORE

answered Sep 16, 2022 in Database by narikkadan
• 63,600 points
1,151 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP