Count only 1 if there's a row data exist? - mysql

I have this capstone project that need to finish tomorrow. They suggest that I should count the number of student violators by gender/course/violation.
the only problem is, is there a query that if a 2 or more violation offense has been committed by the student, the result will only show that counts as 1 student violator.
This is the image sample of my student violation table.
Sorry for my bad grammar.

I would use SELECT DISTINCT for such cases.
For example:
SELECT DISTINCT StudentNumber FROM my_table WHERE TypeOfViolation="SomeType";
You can refere here for more details:
https://www.w3schools.com/sql/sql_distinct.asp

Related

How to implement SQL join on multiple keys for two tables using where clause?

I am interested to find the student headcount of 1st week of fall 2018 for the Business faculty by sex using the following two tables. Here ID is the primary key or part of the primary key for each of the data tables. In plan table, the program is the student's college(for our case, "Business"), Sex has values M, and F, TERM has the values, "Fall", "Spring", and " Summer", Sessions has the values, " 1st week", and " 2nd week". I was wondering if you could check my code. I am kinda new in SQL! I sincerely appreciate your time.
My SQL code:
SELECT count(Student.ID) as COUNT, Student.Sex as Sex
FROM Student JOIN Plan
on Student.ID=Plan.ID
WHERE Student.Term="Fall"
AND Student.Sessions="1st Week"
AND Plan.Program="Business"
GROUP BY Student.Sex;
The answer to this depends on whether or not the ID column in the plan table is the student’s id number , which i believe it is not. The plan table should have an entry for each student session combination, in order to account for the same student having different sessions. See: one to many relationship. From there, you should be able to execute this query after you group by all of the fields mentioned in the select clause.
Anyway, I've created the fiddle for you reference here :
https://www.db-fiddle.com/f/7G2DwLB6SNqbAtKeD96nWG/1
And I can conclude that based on you query condition given, it is correct. But like #Benny mention, the field ID is usually referring to the tables data ID. For example ID in Student is the student id while in Plan its highly possible that its ID for each plan. But you said it's identical AND there's no student_id column in the Plan table, so it should be ok.

How to count in a SQL list

I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;

Selecting with multiple conditions in a Single SQL query

Table name is Student
I need the SQL query which gives names of students who are Math majors with more than 30 credits.
Thanks!
select *
from Student
where major='Math' and credits>30;
In MySQL
select firstName, lastName from Student where major='Math' and
credit>30;
Use Multiple conditions in WHERE Clause :
SELECT firstName, lastName
FROM Student
WHERE major='Math' AND credit>30;
select *
from student
where major ='Math' and credit> 30
Like others have rightly answered, the query should look like this and nothing more:
SELECT firstName, lastName FROM Student WHERE major='Math' AND credits>30;
It is worth nothing that, since you are mainly interested in just the full names of the students in the result, being specific on the name column is what is needed in the query and not SELECT ALL. This will greatly improve response time if you are working with a huge database.

SQL Subquery Exercises: Display the salesmen which name are alphabetically lower than the name of the customers

This is the question on w3resource:
http://www.w3resource.com/sql-exercises/subqueries/sql-subqueries-inventory-exercise-21.php
You can see the content of the table on the link above.
This is my solution:
SELECT *
FROM SALESMAN S WHERE S.SALESMAN_ID IN
(
SELECT C.SALESMAN_ID
FROM CUSTOMER C
WHERE S.NAME < C.CUST_NAME
);
And I recieved only one salemen named Jame Hoog.
This is the website's solution:
SELECT *
FROM salesman a
WHERE EXISTS
(SELECT *
FROM CUSTOMER b
WHERE a.name < b.cust_name);
I don't really understand why do they use EXISTS and I think my solution logically is still the same as their solution. Please tell me what's wrong with my solution. Please
The two queries are not the same, as I will explain. There is nothing wrong with your query or your single record result set, as this demo shows:
Rextester
This query is finding every salesman which has at least one customer whose name is lexicographically greater than his own. Only salesman James Hoog has a name which is lexicographically less than any of his customers.
On the other hand, the query from the website is looking for salesman names which are lexicographically less than any customer (either his own or someone else's) from each record in the Salesman table. This is a much more lax requirement than what is used in your query. The logic here is to retain any salesman for which there exist any customers whose name is greater than that of the salesman. Only Pit Alex and Paul Adam fail to meet this requirement, there being no customers whose last name begin with the letter 'P' or any letter greater than that.

get duplicate records in mysql on the basis of multiple columns

I have a table with columns names
ID,EMP_NAME,DEPARTMENT,VOTER ID, MOBILE NO,Driver_License_NO,REGISTRATION_DATE
No employee can be register with more than one department. If any employee got registered with more than department, it would be consider as DUPLICATE record. Duplicate record can judge on the basis of duplicate mobile no. or voter id no.
I want an output like this
Name | Previous Department | Current Department | Possible Reason for Duplication(Mobile or Voter ID)
Sorry for poor English
thanks
The correct way to do this is to have the database do the checking. So, after you clean your data, you should add a unique index. Something like this (I'm not sure what defines an employee):
create unique index unq_atable_empname_department on atable(empname, department);
You can get all the duplicates using aggregation:
select empname, group_concat(distinct department) as departments
from atable
group by empname
having count(distinct department) > 1;
As for possible reasons, you should probably ask another question. Provide sample data and explain what you mean by the possible reasons. This answer is for the gist of the question, identifying duplicates.