select Query with sum - sql

I have a table with 4 columns named StudentId,StudentName,SubjectNAme,SubjectMark.
I want write a Query to get a view with two Fields Named StudentName and Total. The Total is Obtained by Summing the subject marks of corresponding students.

SELECT StudentName,
sum(SubjectMark)
FROM table_name
GROUP BY StudentId,
StudentName

Syntax:
SELECT SUM(column_name) FROM table_name;
column_name is the name of coloumn you want to sum
table_name is table name.

Related

Group by count of distinct values within a table

This is in oracle. Table EmployeeName:
EmployeeNameID|EmployeeID|FirstName|LastName
1|1|ABC|DEF
2|1|ABC|EFG
3|1|ABC|DEF
4|2|XYZ|PQR
5|2|DEF|RST
6|3|XYQ|BRQ
I want to find out how many employee records have more than one name. The result should be: First column is the EmployeeId and the 2nd column is the distinct number of names they have. For the first result the ABC|DEF repeats so I just want to count it once.
1|2
2|2
3|1
I tried to group by but not sure how to work with distinct names requirement.
Try this:
SELECT EmployeeID, COUNT(DISTINCT CONCAT(FirstName,'-', LastName))
FROM EmployeeName
GROUP BY EmployeeID;
I think you just want count(distinct):
select employeeid, count(distinct firstname || ' ' || lastname)
from t
group by employeeid;

Insert record into table using default values and values selected from another table using where clause

I know it is possible to insert records into a table by using a select statement on a different table, but I need to use a where clause to select which record. For example,
INSERT INTO Employee_Archive(EmployeeID, Name, ArchiveReason)
SELECT EmployeeID FROM Employees, Name from Employees, 'Retired'
WHERE EmployeeID = '001'
I hope that example makes sense. I wish to get the EmployeeID and the Name from the Employees table, and add my own ArchiveReason value, but I need to specify by which EmployeeID. Cheers
You can simply add a WHERE clause in your SELECT statement:
SELECT
EmployeeID,
Name,
'Retired'
FROM Employees
WHERE EmployeeID = '001'

how to write this SQL? specifics inside

Assume we have a table which records grades of all students of a class. There are two columns among others in the table: 1) student_id, 2) grades, The value of grades is a single letter which can be "A", "B", "C" or "F". How to write a SQL listing all student ids (one student per line) who has never got a "B" grade? Thanks.
PS: Assume we are using MySQL.
You would need a subquery to accomplish this. You'll return records from the table where the student_id is not in the list of students who have received a B.
select student_id, grades
from table_name
where student_id not in (select student_id from table_name where grade = 'B')
Use this and sorry, you need to define that student_id can be repeated in table
select student_id from table_name where grades in ('A','B','C')
and student_id not in(
select student_id from table_name where grades = 'B'
)

How can I select distinct number of values from a column in SQL?

The database contains a REGISTRATION table and STUDENT table.
The columns of REGISTRATION table are :
CourseID, StudentID, CourseCode, Score, Year
The CourseCode column contains the codes of courses like CS-101, MS-202 (each student ID is registered to many courses). I need to find the names and ID's of students taking more than 3 courses.
I have tried:
Select distinct
CourseRegistrations.S_ID, Students.FirstName
from
Students, CourseRegistrations
where
Students.StudentID = CourseRegistrations.StudentID
group by
CourseRegistrations.S_ID, Students.FirstName
having
count(distinct CourseRegistrations.CourseCode) > 3
but this is showing all records of file.
You should try:
Select CourseRegistrations.StudentID, count(CourseRegistrations.CourseCode)
from Students, CourseRegistrations
where Students.StudentID=CourseRegistrations.StudentID
GROUP BY CourseRegistrations.StudentID
having count(CourseRegistrations.CourseCode)>3
Distinct is not need if you used GROUP BY
Select CusReg.S_ID
from Students stud join CourseRegistrations CusReg
ON stud.StudentID=CusReg.StudentID
GROUP BY CourseRegistrations.S_ID
having count(CusReg.CourseCode)>3
Dont Use OLD Style Joins
SOURCE

How to get all the columns, but have to group by only 2 columns in sql query

I have a table Employees, which has Fields as below:
Employee_name,Employee_id,Employee_status,Employee_loc,last_update_time.
This table does not have any constraint.
I have tried the below query.
select Employee_name, count(1)
from Employees
where Employee_status = 'ACTIVE'
Group by Employee_name,Employee_loc
having count(Employee_name) > 1
order by count(Employee_name) desc
In the select, I need to get Employee_id too.. Can any one help on how to get that?
You can just add Employee_id to the query, and also add it to the group by clause. (Adding it to the grouping won't make any difference in the query results, assuming each employee name each employee id is unique).
If the grouping does make a difference, that implies that some combinations of employee name and location have more than one ID associated with them. Your query would therefore need to decide which ID to return, possibly by using an aggregate function.
SELECT EMPLOYEE_NAME, EMPLOYEE_ID, COUNT(1)
FROM
EMPLOYEES
WHERE
EMPLOYEE_NAME IN
(
SELECT EMPLOYEE_NAME
FROM EMPLOYEES
WHERE Employee_status = 'ACTIVE'
GROUP BY Employee_name,Employee_loc
HAVING COUNT(*) > 1
)
GROUP BY EMPLOYEE_NAME, EMPLOYEE_ID
You can also use partition by clause and select whichever columns you want to see irrespective of the columns you are using for aggregation.
A very short and simple explanation here - Oracle "Partition By" Keyword