| |||||||
| Coding / Scripting / Programming Discussions on all manner of coding and scripting. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) | |
| Dose anyone know SQL? IÂ’m taking a SQL concepts class using Microsoft Access and I canÂ’t for the life of me figure out one of the problems on this assignment. Any help would be appreciated. (6 points) For all students, list the student name in the first column. In the second column display 'Yes' if the student has enrolled in any course. Display 'No' in the second column if the student has not enrolled in any courses. Sort the results by student name. NOTE: It is possible for a student to have a NULL GPA even if they have enrolled in a course (they may not have completed the course yet!). Here is the Database and discription this problem uses. | ||
| | | |
| | #4 (permalink) | |
| i came up with this('.' are just formating place holders): SELECT stuname, (SELECT 'Yes' .........................................FROM entroll .....................................WHERE EXISTS (SELECT stuid .................................................. .......................FROM enroll .................................................. ...................WHERE enroll.stuid = student.stuid)) ....................................UNION .................................(SELECT 'No' ......................................FROM entroll ..................................WHERE NOT EXISTS (SELECT stuid .................................................. .............................FROM enroll .................................................. .........................WHERE enroll.stuid = student.stuid)) FROM student ORDER BY stuname; but it dosnt work. depending on how i do the parenthases around the sub-querie it gives me a few different error messages. I turned the assignment in, but if i could still get some help with it i could turn the on problem in late. Thanks godfoot Last edited by Demon Made; 12-November-05 at 09:55 AM.. | ||
| | | |
| | #5 (permalink) | ||||||||||||||||||||||||
Well, I admit that my SQL knowledge is limited to what I have learned in the last few months, but what you have looks pretty close except for the text in red having a typo. I am not sure either whether that typo was in the query that you submitted. Only other thing(s) I could think of are potential missing semicolons. | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #6 (permalink) | |
| when i run that query i get this error: "Query input must contain at least one table or query." I have no idea what this means. Oh, and yeah that misspell was in the query but apparently the assebler didnt get that far in running it to notice. Last edited by Demon Made; 14-November-05 at 06:27 PM.. | ||
| | | |
| | #7 (permalink) | ||||||||||||||||||||||||
If the table you are trying to query was misspelled, you will always get that error as it cannot find the table it is looking for since it is misspelled. Have you tried rerunning the query with the proper spelling? | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
| | #12 (permalink) | |
| Here's one that works, but it may use some things the teacher hasn't covered yet (they don't often like when you get too far ahead of 'em ).Code: SELECT StuName, (IIF(SUM(Enroll.StuId) > 0, 'Yes', 'No')) AS Enrolled FROM Student LEFT JOIN Enroll ON Student.StuId = Enroll.StuId GROUP BY StuName ORDER BY StuName; Code: SELECT stuname, ((SELECT 'Yes'
FROM enroll
WHERE EXISTS (SELECT stuid
FROM enroll
WHERE enroll.stuid = student.stuid)
UNION
(SELECT 'No'
FROM enroll
WHERE NOT EXISTS (SELECT stuid
FROM enroll
WHERE enroll.stuid = student.stuid)))
FROM student
ORDER BY stuname;
Code: SELECT StuName, 'Yes' AS Enrolled
FROM Student
WHERE EXISTS (SELECT StuId
FROM Enroll
WHERE Enroll.StuId = student.StuId)
UNION ALL
SELECT StuName, 'No' AS Enrolled
FROM Student
WHERE NOT EXISTS (SELECT StuId
FROM Enroll
WHERE Enroll.StuId = Student.StuId)
ORDER BY StuName;
Note: I used "UNION ALL" in the last query instead of just plain "UNION", since it'll run faster for large queries. "UNION" attempts to eliminate any duplicate rows. But since you know there are no duplicates (aren't any No's in the first query or Yes's in the second query), "UNION ALL" will work the same and run faster. Last edited by FunkyFresh; 15-November-05 at 09:34 AM.. | ||
| | | |
| | #13 (permalink) | ||||||||||||||||||||||||
Good idea on the UNION ALL when I was working on a database, I was looking for something a little faster, and this would of helped me out greatly. | |||||||||||||||||||||||||
| | | ||||||||||||||||||||||||
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |