Selecting with multiple conditions in a Single SQL query - mysql

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.

Related

Wrong SQL query (beginner)

I am new in SQL, and I am learning alone on Udemy. I came across multiple questions, and I am struggling with one of them:
What is wrong with this SQL query?
SELECT sport, count(*)
FROM activities
WHERE username IN ‘tony’
GROUP BY 1;
I have two hypothesis:
1 - If the field 'sport' in activities is filled with string values, then we can't use count.
2 - the last statement should be rather:
WHERE username in:‘tony’ GROUP BY 1;
I would be happy to have your feedback on the question and learn from you!
Thanks
search IN will use with () round brackets
SELECT sport, count(*)
FROM activities
WHERE username IN ('tony')
GROUP BY 1;
Add parentheses for tony,
example: if u want to check for multiple names ('tony','stark')
SELECT sport, count(*)
FROM activities
WHERE username IN ('tony')
GROUP BY 1;

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

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

how to find an amount of certain duplicates in mysql table?

So I have a table with persons.
This table connects with vehicles with manyToMany.
I need to find how many times does id appear on the table person_vehicle.
How can I do this?
Thanks everybody for asking
Here are two suggestion.
For all the names:
Gives number of times the names are repeated.
SELECT count(name) FROM persons GROUP BY name;
Specifically for the jake:
SELECT count(name) FROM persons WHERE name = "Jake";
What have you tried? Depending on the table, you can do something like:
SELECT * FROM persons WHERE first_name = 'Jake'
Which will return the number of rows with the first name of "Jake"
Alternatively, if you only want one row with the total count of occurrences:
SELECT COUNT(first_name) FROM persons WHERE first_name = 'Jake'
You're looking for GROUP BY.
A table contains multiple columns. You have to decide first which column(s) of the data that you need are in common.
Suppose I have a table address(person_name, plot_no, pin);
If I try to select, for a particulate pin, how many people are living there:
SELECT pin, COUNT(*)
FROM address
GROUP BY pin;
If I try to select, for a particulate plot_no and pin, how many people are living there:
SELECT plot_no, pin, COUNT(*)
FROM address
GROUP BY plot_no, pin;
Use group by statement on the column, on which you are looking for duplicates if the count comes more than 1, it means they are repeating.
Select count(id), name from table_name group by id HAVING count(id) > 1;
Only the duplicates records will come.
Try this:
SELECT count(name) FROM persons WHERE name = "Jake";

Selecting specific records to run query on

I am trying to select a small number of records in a somewhat large database and run some queries on them.
I am incredibly new to programming so I am pretty well lost.
What I need to do is select all records where the Registraton# column equals a certain number, and then run the query on just those results.
I can put up what the db looks like and a more detailed explanation if needed, although I think it may be something simple that I am just missing.
Filtering records in a database is done with the WHERE clause.
Example, if you wanted to get all records from a Persons table, where the FirstName = 'David"
SELECT
FirstName,
LastName,
MiddleInitial,
BirthDate,
NumberOfChildren
FROM
Persons
WHERE
FirstName = 'David'
Your question indicates you've figured this much out, but are just missinbg the next piece.
If you need to query within the results of the above result set to only include people with more than two children, you'd just add to your WHERE clause using the AND keyword.
SELECT
FirstName,
LastName,
MiddleInitial,
BirthDate,
NumberOfChildren
FROM
Persons
WHERE
FirstName = 'David'
AND
NumberOfChildren > 3
Now, there ARE some situations where you really need to use a subquery. For example:
Assuming that each person has a PersonId and each person has a FatherId that corresponds to another person's PersonId...
PersonId FirstName LastName FatherId...
1 David Stratton 0
2 Matthew Stratton 1
Select FirstName,
LastName
FROM
Person
WHERE
FatherId IN (Select PersonId
From Person
WHERE FirstName = 'David')
Would return all of the children with a Father named David. (Using the sample data, Matthew would be returned.)
http://www.w3schools.com/sql/sql_where.asp
Would this be any use to you?
SELECT * from table_name WHERE Regestration# = number
I do not know what you have done up to now, but I imagine that you have a SQL query somewhere like
SELECT col1, col2, col3
FROM table
Append a where clause
SELECT col1, col2, col3
FROM table
WHERE "Registraton#" = number
See SO question SQL standard to escape column names?.
Try this:
SELECT *
FROM tableName
WHERE RegistrationNo = 'valueHere'
I am not certain about my solution. I would propose You to use view. You create view based on needed records. Then make needed queries and then you can delete the view.
View description: A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
Example:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
For more information: http://www.w3schools.com/sql/sql_view.asp

How to display the first and second Doctor to see a patient?

I have to create a table where there are lots of columns pulling date from one table and two views. To display the columns is no problem. The problem comes into play when I have to alias two columns to show a patient who received services from the first two distinct doctors (doctor_1 and doctor_2). Each doctor has their own ID. I’m not sure if I should use the “distinct top (2)”, “rank”, etc… Here is what it sort of looks like.
Date, LastName, FirstName, Address, City, State, Zip, Hostital, Room, Doctor_1,Doctor_2
Any help would be greatly appreciated.
Let's assume your table looks something as follows:
doctor_patients:
doctor_id int,
patient_id int,
visit_date datetime
In that case you can write a query similar to the following:
select a.patient_id, b.doctor_id as doctor_1, b.visit_time_max as doctor_1_visit_time, a.doctor_id as doctor_2, max(a.visit_time) as doctor_2_visit_time
from doctor_patients a
inner join
(select patient_id, doctor_id, max(visit_time) as visit_time_max
from doctor_patients
where visit_time < a.visit_time
and patient_id = a.patient_id
and doctor_id = a.doctor_id) b
You can always put a where clause at the tail end and apply filters to the "a" table. I am not sure if this query will run as-is because I did not try it out on SQL. But I hope this gives you a general idea about what you need to do. There might be better ways to accomplish this using latest SQL features but this should get you rolling. I hope this is the answer you were looking for!