Recently, I was invited to a local college for first-year students to help students with exposure to SQL Server. I had a great time with them. I have created a very simple data table and also created 10 queries, which we can learn together, and they can practice later. I thought it was a very good exercise for everyone. Here, I will be sharing the same. Let us see Absolute Beginners 10 Queries.
Sample Table Creation
CREATE TABLE Students ( ID int, Name varchar(50), GradeLevel int, GPA decimal(3,1) ); INSERT INTO Students VALUES (1, 'John Doe', 9, 3.5), (2, 'Jane Smith', 10, 3.2), (3, 'Bob Johnson', 10, 2.9), (4, 'Sue Thompson', 11, 3.8), (5, 'Mike Williams', 11, 3.4), (6, 'Sarah Davis', 12, 3.7), (7, 'Phil Miller', 12, 3.9), (8, 'Amy Martin', 12, 3.6);
Here are the 10 questions that we discussed and learned about their answer.
- What are the names of students in grade 10?
- How many total students are there?
- What are the names and GPAs of students ordered by GPA descending?
- What is the average GPA for each grade level?
- Which students have a GPA greater than 3.5?
- Who are the top 2 students based on GPA?
- How many students are there per grade level?
- Which grade levels have more than 1 student?
- Who is the student with the highest GPA?
- What are the overall average GPA and number of students?
Absolute Beginners Answers
Now, let us look at the questions and their answers:
-- Query 1: -- Returns the names of students in grade 10 SELECT Name FROM Students WHERE GradeLevel = 10 -- Expected result: -- Name -- Jane Smith -- Bob Johnson -- Query 2: -- Returns the total number of students SELECT COUNT(*) AS NumStudents FROM Students -- Expected result: -- NumStudents -- 8 -- Query 3: -- Returns student name and GPA sorted by GPA descending SELECT Name, GPA FROM Students ORDER BY GPA DESC -- Expected result: -- Name, GPA -- Phil Miller, 3.9 -- Amy Martin, 3.6 -- Sarah Davis, 3.7 -- Sue Thompson, 3.8 -- Mike Williams, 3.4 -- Jane Smith, 3.2 -- Bob Johnson, 2.9 -- John Doe, 3.5 -- Query 4: -- Returns grade level and average GPA for each grade level SELECT GradeLevel, AVG(GPA) AS AverageGPA FROM Students GROUP BY GradeLevel -- Expected result: -- GradeLevel, AverageGPA -- 9, 3.50 -- 10, 3.05 -- 11, 3.60 -- 12, 3.73 -- Query 5: -- Returns all student data with GPA greater than 3.5 SELECT * FROM Students WHERE GPA > 3.5 -- Expected result: -- StudentID, Name, GradeLevel, GPA -- 1, John Doe, 9, 3.5 -- 4, Sue Thompson, 11, 3.8 -- 6, Sarah Davis, 12, 3.7 -- 7, Phil Miller, 12, 3.9 -- 8, Amy Martin, 12, 3.6 -- Query 6: -- Returns top 2 students by highest GPA SELECT TOP 2 * FROM Students ORDER BY GPA DESC -- Expected result: -- StudentID, Name, GradeLevel, GPA -- 7, Phil Miller, 12, 3.9 -- 8, Amy Martin, 12, 3.6 -- Query 7: -- Returns count of students for each grade level SELECT GradeLevel, COUNT(*) AS NumStudents FROM Students GROUP BY GradeLevel -- Expected result: -- GradeLevel, NumStudents -- 9, 1 -- 10, 2 -- 11, 2 -- 12, 3 -- Query 8: -- Returns grade levels that have more than 1 student SELECT GradeLevel FROM Students GROUP BY GradeLevel HAVING COUNT(*) > 1 -- Expected result: -- GradeLevel -- 10 -- 11 -- 12 -- Query 9: -- Returns name and GPA of student with highest GPA SELECT TOP 1 Name, GPA FROM Students ORDER BY GPA DESC -- Expected result: -- Name, GPA -- Phil Miller, 3.9 -- Query 10: -- Returns overall GPA average and total number of students SELECT AVG(GPA) AS AverageGPA, COUNT(*) AS NumStudents FROM Students -- Expected result: -- AverageGPA, NumStudents -- 3.475, 8
If you are a beginner with the SQL Server, you can use the sample database and try to write answers to the sample questions with any of your favorite SQL tools. I personally believe it will be a great experience. You can further follow me on Twitter.
Reference: Pinal Dave (https://blog.sqlauthority.com)
First appeared on SQL SERVER – Absolute Beginners 10 Queries