Sample Database: School Management System: Let's Start by Creating A Database
Sample Database: School Management System: Let's Start by Creating A Database
We will work on a sample database and understand several basic SQL commands like SELECT,
WHERE, ORDER BY and much more.
You can go ahead and create a database in pgadmin by left clicking on databases and clicking
on CREATE DATABASE, you can choose to name a database as SchoolDB and add some
comments over there.
Think of it like building a new school, where you'll keep all the information about students and
teachers.
Creating Tables:
We will be creating two tables(Student and Teachers Table) which will be used as a
demonstration of commands out there.
This command creates a table called 'Students' with columns for student ID, name, age, and
class.
CREATE TABLE: This command is used to create a new table in the database.
(...) contains the columns and data types for the table. Each line in the parentheses defines a
column.
Column Definitions in the Students Table:
● StudentID INT PRIMARY KEY:
○ StudentID: Name of the column.
○ INT: Data type integer, used for whole numbers.
○ PRIMARY KEY: This signifies that each value in this column is unique and
identifies each row in the table.
● Name VARCHAR(50):
○ Name: Column name.
○ VARCHAR(50): Data type for variable-length strings (text). 50 specifies the
maximum length.
● Age INT:
○ Age: Column name.
○ INT: Data type integer, used for whole numbers.
● Class VARCHAR(10):
○ Class: Column name.
○ VARCHAR(10): Data type for variable-length strings, with a maximum length of 10
characters.
● This creates a 'Teachers' table with teacher ID, name, and the subject they teach.
● Here, TeacherID serves as a unique identifier for each teacher.
Now, we have created tables in the databases but we need to insert them right? So for that
reason we will insert some sample data into both the tables.
Into students:
SELECT Class:
● SELECT: This keyword is used to specify which columns you want to
retrieve from the database.
● Class: This is the name of the column in the Students table. The query
will return the value from the Class column.
COUNT(*):
● COUNT: This is an aggregate function in SQL. Aggregate functions perform
a calculation on a set of values and return a single value.
● (*): This symbol represents all rows. When used with COUNT, it counts all
rows in the specified column or in the table if no column is specified.
● In this context, COUNT(*) counts all rows (or students) in each group
defined by the GROUP BY clause.
as StudentCount:
● as: This is used to create an alias in SQL. An alias is a temporary name
assigned to a column or a table in your SQL query.
● StudentCount: This is the alias name given to the result of the COUNT(*)
function. Instead of showing a column name like COUNT(*) in the results, it
will show as StudentCount.
● This makes the output more readable and understandable. Instead of
seeing COUNT(*), which might be confusing, you see StudentCount, clearly
indicating the meaning of that number.
FROM Students:
● FROM: Specifies from which table the SQL engine should retrieve the data.
● Students: The name of the table where the query is being applied.
GROUP BY Class:
● GROUP BY: This clause groups rows that have the same value in the
specified column(s), allowing aggregate functions like COUNT, SUM, AVG,
etc., to be applied to each group.
● Class: Indicates that the data should be grouped by the Class column.
This means the COUNT(*) operation will be performed separately for each
class.
Using LIMIT:
Creates a temporary set of senior students and then selects their names. Here it’s Isolating and
working with a specific group of students.