Identify wrong data [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
We discovered wrong data in one of our table and we would like to identify them.
We have 4 tables:
Student
School
School_Student
Location
The relation between them as follow
Each location has many schools
Each student can be in different schools in the same location
Due that some students have the same name although they are different persons with different ID, we found that some students are assigned to different schools in different locations
You can see in the screenshot that "Adam Mike" is assigned to 3 different schools, the second and the third line are ok because the location of the school is the same however the first line is not ok because the school is in a different location.
so i would like to have a query that returns all students that are assigned to different locations so that i can manually correct them.
Thank you
EDIT
In my last question Find duplication in multi tables
the student cannot be in different school.
To make it more clear.
In this screenshot you see that all records with "?" are not correct because the location is different.
So mike cannot be in a school in USA and in GB
in the otherhand JIM is in different schools but in the same location so this is ok
Now i would like to find all records where students has records in different locations.

Try something like. It returns the students that have more than 1 unique location.
SELECT ss.student_id
FROM school s
JOIN school_student ss
ON s.id = ss.school_id
GROUP BY ss.student_id
HAVING COUNT(DISTINCT s.location_id) > 1

Related

Designing a database for private clinic [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am using db 18C XE AND APEX 21.1. I am designing a database for private clinics. A private clinic is a place where patients get diagnosed. The clinic has only one doctor and an assistant or two. The assistant records the patients' data and visits(admission) data. The patient gets diagnosed at the first visit and get a prescription with the drugs they should take. Then a follow-up or more visits follows afterward i.e the second visit is after two weeks then a third one after a month and so on. Each follow-up has a new prescription or maybe the same prescription if the doctor thinks there is no need for a new drug. The patient could be diagnosed for a disease then gets cured after a number of visits. Then he could visit the clinic again after a year or two for a different complain. The same process occurs again. I am attaching a photo of the part of the visits, examining and follow-ups. I want to be sure if I designed it correctly.
As far as I am able understand your use case, the follow up table will record the follow up visits of the patient. Since you have columns like dose, drug_id etc in FOLLOW_UP_PRESCRIPTION table, I think there must be a patient id as well to map drugs/medicines with the patient.
Correct me if I'm missing something

explaining an SQL query

If I was given a question in an exam, where I would explain a piece of code from Microsoft Access SQL Query, without any external data or information.
How would I explain the following code in simple/plain English, I just want to get an idea because this is my first time explaining a code:
SELECT last_name, department_id, job_id
FROM employees
WHERE department_id IN
(
SELECT department_id
FROM departments
WHERE location_id = 1700
);
Is this a good explanation to a layman:
The following code: asks us to list employees' last name, their job at each department and their job identity. For each department that has a location ID equals/equivalent to '1700'. Would this be a great explanation?
SQL means Structured Query Language. Another word for "Query" is just "Question". Just about every query is written to provide a set of data that answers a question.
When you're explaining a code sample, or trying to figure out some code you inherited for that matter, a good place to start is to start by trying to figure out what question the results of the query were intended to answer. If you were sitting at your computer and somebody came up and asked you a question, what question would that be that would result in you writing that block of code? Oftentimes, just that question alone is enough of an explanation for a layperson.
Here, I'd say a fair start would be something along these lines.
This code answers the question, "What are the last names, department IDs and job IDs of everyone whose department is at location 1700?"
When giving a technical explanation, you take it to the next step. How does this code go about answering the question that was asked? This is the nuts and bolts of how the query works, as opposed to what it's supposed to do.
So, technically:
This code uses a sub-query against the departments table, which includes a location_id column, to identify all the departments at location_id = 1700. Then it uses the department list from the sub-query to get the last_name and job_id data from the employees table, using the department_id list as a filter.
As per my point of view, you need to explain the following thing
This query type is a subquery
This query provides the employee list.
This employee list is base on department and only those department need to consider which location id is 1700
So in a short way, you can say like this query give a list of employee which work on the specific location.
I hope you got this answer as correct answer.
I think a pithier explanation would be along the lines:
Fetch certain information about employees whose department is at location 1700.

N-to-N relation with ability to alsp show items not associated with any [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have 3 tables inside my SQL database: users, groups and users_in_groups (N-to-N relation table).
When user is requesting a list of other users, he can get only contacts which are associated with his groups. To handle it I am getting IDs of user's groups and looking for other users in groups with this IDs.
But now I need to add list of users who are not associated with any group.
Solution I am thinking about is to make separate request to know user IDs which are not listed in users_in_groups table by adding NOT IN (user IDs from users_in_groups).
If you want users not in any groups, you can use not exists:
select u.*
from users u
where not exists (select 1
from user_in_groups ug
where ug.user_id = u.user_id
);

Database design for an online quiz

I am designing an online math quiz for a college project and having some trouble with designing my database.
The basic idea of the website is as follows:
A teacher, once registered may log in and add questions to their account. They can choose between making the questions multiple choice OR true or false. They can also choose between making their questions public or private. (If they choose to make the questions public, other teachers may view the questions.)
At any time the teacher may create a quiz for their students using the questions in their private bank and / or questions from the public bank. Each question may be used in multiple quizzes.
The idea is that the students will later log in and do the quiz; their answers are stored and the teacher can generate reports and check how the students did individually / highest and lowest scoring questions etc.
I am having some trouble deciding how to store the quizzes and questions which I hope somebody may be able to help me with.
So far I have the following:
‘Question’ table with attributes: QuestionID, SubjectArea, Concept, QuestionText, TeacherID, QuestionType, PublicYorN
‘MCQuestions’ table with attributes: QuestionID, AnsA, AnsB, AnsC, AnsD, AnsE, CorrectAns
‘TorFQuestions’ table with attributes: QuestionID, CorrectAns
‘Quiz’ table with attributes: QuizID, CreationDate, TeacherID
I think I then need another table as follows:
‘QuizQuestions’ and the only attributes will be QuizID, QuestionID which together make a concatenated primary key.
I feel like I should have a separate table to store answers to questions and I'm not sure if I need to separate true or false questions and multiple choice questions as I have done above.
(Obviously there are other tables containing user data etc. but this is the part I’m concerned with.)
Any advice / input is greatly appreciated!
A simple yet flexible design would be something like this:
Questions table (id, text, correct answer id, all the other question related data)
Answers table (id, question id text, all the other answer related data)
Quiz table (id, text, all the other quiz related data)
Quiz questions table (quiz id, question id, question display order, other related data such as question weight might also be added)
Quiz results table (quiz id, question id, answer id, user id, all the other related data such as date of answer and such).
I think my only input to this open ended question is:
Combine MCQuestions and TorF Questions to have a different format. Answers table has 4 cols. QID, AnswerID,answer,Correct Y/N.
Then a true or false question has 2 rows - eg. QID=888, AID=1,Ans=TRUE,true. Next row is QID=888, AID=2,Ans=FALSE,false.
Multiple choice has several - so which is the bright yellowy thing? QID=889, AID=3,Ans=Moon,false. QID=889, AID=4,Ans=Sun,true. QID=889, AID=5,Ans=Mars,false.
Then you grab a list of answers and can populate a set of radio buttons with the QID and AID values. Then you are matching integers in your code rather than long text strings which will need to be passed around for matching. Might make it a bit easier in case of special chars in the answers or whatever and you're matching the quiz results to a unique key.
Apart from that minor change it's difficult to help without knowing any other constraints...
Nick

Merging SQL views [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working for the IT Department of a college as a student worker and have very limited knowledge in using SQL (I am not majoring in Computer Science/Engineering). I'll try my best to describe what I want to accomplish here.
I want to create a table that includes info about new students, basically : id, first name, last name, need exams (Y/N), Course101 section, instructor.
My problem is, exchange and transfer students and also some first year students would not have to/did not sign up for Course101, so using WHERE studnt_course = 'Course101%' will leave out those students. I would like to pick up those students in my view and display their Course101 section, Instructor values as Null.
I am thinking about making two views, one for all new students, and one for students with Course101 only, and do some kind of merging/union but not sure how to actually do that.
Any help will be greatly appreciated!
It's still a bit vague what the current tables actually look like which makes it hard to give a good suggestion.
Based on what you've given us I'd suggest looking into a LEFT INNNER JOIN which would put NULL where the two tables don't overlap.
If you are interested in learning database design (rather than just solving this particular problem) I'd suggest looking into proper database design.
Create two queries. One for the 101 students and one for the overseas/transfers (you will need to work out what the WHERE clause if for that)
Get each one working to your satisfaction.
If every thing is the same apart from the WHERE clause then:
Then grab the conditions for each, wrap them in brackets and put an OR in between, put that into one query
So something like:
SELECT Name, Id,ShoeSize
FROM Students
WHERE (studnt_course = 'Course101%') OR (transferred = 1 OR is_exchange = 1)
i.e., So all students WHERE <this> OR <that> is true
Other wise (e.g., using diffrent tables):
Make sure that you are selecting the same column names in you SELECT statement
If one column has the same info but is call some thing different you can go:
<columnName> AS <NameYouWantToCallIt>
Make sure the column names are in the same order.
Then put the word UNION in-between the two queries.
This will combine the results from both queries (and remove duplicates).
SELECT Name, Id, ShoeSize
FROM Students
WHERE studnt_course = 'Course101%'
UNION
SELECT Name, Id, FootSize AS ShoeSize
FROM exchangeStudents
WHERE transferred = 1 OR is_exchange = 1