Meera is a forest ranger who protects wildlife in a nature reserve. Sheβs searching for the rare Shadow Leopard , which hasnβt been seen in months. To help track sightings and share data with scientists, she uses a simple database with three tables: rangers
, species
, and sightings
. These help her log observations, learn about endangered animals, and find clues about rare animals like the Shadow Leopard might be hiding.
This assignment focuses on PostgreSQL database operations using a real-world scenario in wildlife conservation monitoring. You will create and manage three tables (rangers
, species
, sightings
), insert sample data, and perform essential SQL queries including:
- CRUD operations
- Constraints (PK, FK, CHECK, DEFAULT)
- JOINs
- Aggregations (
COUNT
,MAX
, etc.) - Filtering (
WHERE
,HAVING
) - Data manipulation (
UPDATE
,DELETE
) etc
This assignment reinforces your understanding of relational databases while engaging with an environmental use case.
1οΈβ£ Install PostgreSQL on your system if not already installed.
2οΈβ£ Open pgAdmin or your preferred PostgreSQL terminal.
3οΈβ£ Create a new database named "conservation_db"
or any appropriate name.
4οΈβ£ Connect to the newly created database.
5οΈβ£ Write PostgreSQL queries to solve given problems.
| Table Name | What It Stores |
| ----------- | ----------------------------------------------------------------------------- |
| `rangers` | Information about rangers like Meera (name, contact, zone) |
| `species` | Info about different animals (name, scientific name, how endangered they are) |
| `sightings` | Records of when and where each animal was seen |
| Field Name | Description |
| ----------- | ------------------------- |
| `ranger_id` | Unique ID for each ranger |
| `name` | Full name of the ranger |
| `region` | Area they patrol |
| Field Name | Description |
| --------------------- | -------------------------------------- |
| `species_id` | Unique ID for each species |
| `common_name` | Common name (e.g., "Shadow Leopard") |
| `scientific_name` | Scientific name |
| `discovery_date` | When the species was first recorded |
| `conservation_status` | Status like "Endangered", "Vulnerable" |
β discovery_date helps track when a species was officially identified.
| Field Name | Description |
| --------------- | ------------------------------------------ |
| `sighting_id` | Unique ID for each sighting |
| `ranger_id` | Who made the sighting (links to `rangers`) |
| `species_id` | Which animal was seen (links to `species`) |
| `sighting_time` | Date and time of the sighting |
| `location` | Where it was seen |
| `notes` | Additional observations (optional) |
β sighting_time tracks when an animal was seen β very useful for monitoring wildlife activity.
| Relationship | Description |
| ---------------------- | ------------------------------------------------- |
| `sightings`β `rangers` | Each sighting is linked to the ranger who made it |
| `sightings`β `species` | Each sighting is linked to a specific species |
| ranger_id | name | region |
|-----------|------------------|--------------- |
| 1 | Alice Green | Northern Hills |
| 2 | Bob White | River Delta |
| 3 | Carol King | Mountain Range |
| species_id | common_name | scientific_name | discovery_date | conservation_status |
|------------|-------------------|-------------------------|----------------|---------------------|
| 1 | Snow Leopard | Panthera uncia | 1775-01-01 | Endangered |
| 2 | Bengal Tiger | Panthera tigris tigris | 1758-01-01 | Endangered |
| 3 | Red Panda | Ailurus fulgens | 1825-01-01 | Vulnerable |
| 4 | Asiatic Elephant | Elephas maximus indicus | 1758-01-01 | Endangered |
| sighting_id | species_id | ranger_id | location | sighting_time | notes |
|-------------|------------|-----------|-------------------|----------------------|----------------------------|
| 1 | 1 | 1 | Peak Ridge | 2024-05-10 07:45:00 | Camera trap image captured |
| 2 | 2 | 2 | Bankwood Area | 2024-05-12 16:20:00 | Juvenile seen |
| 3 | 3 | 3 | Bamboo Grove East | 2024-05-15 09:10:00 | Feeding observed |
| 4 | 1 | 2 | Snowfall Pass | 2024-05-18 18:30:00 | (NULL) |
1οΈβ£ Register a new ranger with provided data with name = 'Derek Fox' and region = 'Coastal Plains'
Sample Output:
AffectedRows : 1
(No output needed - this is an INSERT operation)
2οΈβ£ Count unique species ever sighted.
Sample Output:
| unique_species_count |
| ---------------------|
| 3 |
3οΈβ£ Find all sightings where the location includes "Pass".
Sample Output:
| sighting_id | species_id | ranger_id | location | sighting_time | notes |
| ------------|------------|-----------|---------------|---------------------|--------|
| 4 | 1 | 2 | Snowfall Pass | 2024-05-18 18:30:00 | (NULL) |
4οΈβ£ List each ranger's name and their total number of sightings.
Sample Output:
| name | total_sightings |
|-------------|-----------------|
| Alice Green | 1 |
| Bob White | 2 |
| Carol King | 1 |
5οΈβ£ List species that have never been sighted.
Sample Output:
| common_name |
|------------------|
| Asiatic Elephant |
6οΈβ£ Show the most recent 2 sightings.
Sample Output:
| common_name | sighting_time | name |
|---------------|----------------------|-------------|
| Snow Leopard | 2024-05-18 18:30:00 | Bob White |
| Red Panda | 2024-05-15 09:10:00 | Carol King |
7οΈβ£ Update all species discovered before year 1800 to have status 'Historic'.
Sample Output:
AffectedRows : 3
(No output needed - this is an UPDATE operation)
8οΈβ£ Label each sighting's time of day as 'Morning', 'Afternoon', or 'Evening'.
- Morning: before 12 PM
- Afternoon: 12 PMβ5 PM
- Evening: after 5 PM
Sample Output:
| sighting_id | time_of_day |
|-------------|-------------|
| 1 | Morning |
| 2 | Afternoon |
| 3 | Morning |
| 4 | Evening |
9οΈβ£ Delete rangers who have never sighted any species
Sample Output:
AffectedRows : 1
(No output needed - this is a DELETE operation)
1οΈβ£ Prepare a single SQL file containing:
- SQL code for table creation, sample data insertion, and all queries.
- SQL queries for all problems, each preceded by a comment (
- Problem X
).
2οΈβ£ Verify that all queries run without errors.
3οΈβ£ Save your file as "PostgreSQL_Assignment.sql" or another appropriate name.
πΉ Submit only the GitHub repository link containing your solution file. GitHub repository should be public.
- What is PostgreSQL?
- What is the purpose of a database schema in PostgreSQL?
- Explain the Primary Key and Foreign Key concepts in PostgreSQL.
- What is the difference between the
VARCHAR
andCHAR
data types? - Explain the purpose of the
WHERE
clause in aSELECT
statement. - What are the
LIMIT
andOFFSET
clauses used for? - How can you modify data using
UPDATE
statements? - What is the significance of the
JOIN
operation, and how does it work in PostgreSQL? - Explain the
GROUP BY
clause and its role in aggregation operations. - How can you calculate aggregate functions like
COUNT()
,SUM()
, andAVG()
in PostgreSQL?
π‘ Pro Tip: Don't be short and concise in your answers; explain the idea behind each question and provide in-depth analysis with relevant examples.
Date | Marks | Deadline Time |
---|---|---|
26 May, 2025 | 60 Marks | Until 11:59 PM |
27 May, 2025 | 50 Marks | Until 11:59 PM |
After 27 May, 2025 | 30 Marks | Until 11:59 PM |
Participation in this assignment is mandatory for all students. It builds foundational skills in PostgreSQL, which will be critical for future topics like Prisma ORM and full-stack development.
Approach this task with dedication, precision, and a commitment to excellence.
Plagiarism will not be tolerated. Please make sure that the code you submit is your own. Any instance of plagiarism will result in a score of 0. Additionally, if any AI-generated content or ChatGPT-generated responses are detected, the score will also be 0.
Best of luck! π‘π₯πΏ