{"id":17655805,"url":"https://github.com/manuel7806/sql-server-hospital-database","last_synced_at":"2025-09-06T09:45:04.116Z","repository":{"id":258608206,"uuid":"873840745","full_name":"Manuel7806/sql-server-hospital-database","owner":"Manuel7806","description":"A simple database that people can install and use to practice writing queries with.","archived":false,"fork":false,"pushed_at":"2024-10-17T14:13:53.000Z","size":77,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-06T09:45:03.387Z","etag":null,"topics":["database","databases","sql","sql-server","ssms","t-sql","t-sql-exercises","t-sql-scripts"],"latest_commit_sha":null,"homepage":"","language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Manuel7806.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-16T20:09:30.000Z","updated_at":"2025-06-15T20:24:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"3100ca84-f63d-445b-8be5-e8cd77d4a4fc","html_url":"https://github.com/Manuel7806/sql-server-hospital-database","commit_stats":null,"previous_names":["manuel7806/sql-server-hospital-database"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Manuel7806/sql-server-hospital-database","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Manuel7806%2Fsql-server-hospital-database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Manuel7806%2Fsql-server-hospital-database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Manuel7806%2Fsql-server-hospital-database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Manuel7806%2Fsql-server-hospital-database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Manuel7806","download_url":"https://codeload.github.com/Manuel7806/sql-server-hospital-database/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Manuel7806%2Fsql-server-hospital-database/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273886640,"owners_count":25185509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","databases","sql","sql-server","ssms","t-sql","t-sql-exercises","t-sql-scripts"],"created_at":"2024-10-23T13:08:00.396Z","updated_at":"2025-09-06T09:45:04.082Z","avatar_url":"https://github.com/Manuel7806.png","language":"TSQL","readme":"# SQL Server Hospital Database \u003c!-- omit from toc --\u003e\n\n## Table of Contents \u003c!-- omit from toc --\u003e\n\n- [About](#about)\n- [Install and Use the Database](#install-and-use-the-database)\n  - [Prerequisite](#prerequisite)\n  - [Cloning the Repo](#cloning-the-repo)\n- [Run Queries](#run-queries)\n- [Sample Queries](#sample-queries)\n\n## About\n\nThis project was inspired by [sql-practice](https://www.sql-practice.com/), the key difference is that\nI added an extra table for doctor specializations, created a table for allergies and a table for patient\nallergies with an extra 'reaction' column. This project allows users to install the database and practice\nwriting SQL queries. It can be used to practice basic select, insert, update, and delete statements. You\ncan also practice joins, aggregates, subquries, creating views, triggers, functions, and stored procedures.\n\nThe data within the tables is completely fictional and does not represent any real person or persons.\nThe data for the patients and doctors came from [randomuser.me](https://randomuser.me/), a great site\nfor generating random information for user information. The patient and doctor data in the admissions\ntable is not meant to represent real data, so the doctors' specialties might not line up with the patients'\ndiagnosis, as this is meant for mere learning purposes only.\n\n## Install and Use the Database\n\n### Prerequisite\n\nMS SQL Server - You have to download and install [Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server/sql-server-downloads),\nonce you download it, you can just follow the installation wizard.\n\nSSMS - You have to download and install [SQL Server Management Studio](https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver16). You can follow this [guide](https://learn.microsoft.com/en-us/sql/ssms/quickstarts/ssms-connect-query-sql-server?view=sql-server-ver16) to learn how to connect to a database and how\nto use SSMS.\n\n### Cloning the Repo\n\nOnce you have MS SQL Server and SSMS installed on you computer, you have to clone this repo to your local machine.\n\n```bash\ngit clone https://github.com/Manuel7806/sql-server-hospital-database.git\n```\n\nAfter you have cloned the repo open SSMS go `file` -\u003e `open` and select `folder` and then open the cloned folder.\nYou will have the CSV file along with the script to create and seed the database. Inside the `create.sql`\nscript there is a variable named `DataSourceLocation`, you can rename that variable to the location of where\nyou have the cloned repo at or you can move the cloned repo into the same directory as it is in the script.\n\nOnce you have either changed the variable or moved the repo, make sure you have SQLCMD mode activated in SSMS.\nYou can do that by going to `query` and click on `SQLCMD Mode`. After you have enabled SQLCMD mode you can execute\nthe script and it will create the database objects and insert the data into the tables.\n\n## Run Queries\n\nTo run queries against the database, in the object explorer pane right click on the databases folder and select `refresh`,\nyou should now see the newly created hospital_db database. You can right click on the hospital_db database and select\n`New Query`, a new query window will open in the middle of the window, you can write and run your queries from there.\n\n\n## Sample Queries\n\nList all patients.\n\n```SQL\nSELECT * FROM patients\n```\n\n![list all patients](images/select-all-patients.png)\n\n---\n\nFind patients with a certain allergen.\n\n```SQL\nSELECT\n    p.first_name,\n    p.last_name,\n    a.allergy_name,\n    pa.reaction\nFROM\n    patients AS p\nJOIN\n    patient_allergies AS pa ON p.patient_id = pa.patient_id\nJOIN\n    allergies AS a ON pa.allergy_id = a.allergy_id\nWHERE\n    a.allergy_name = 'Protonix';\n```\n\n![find patients with specific allergy](images/find-patients-with-specific-allergy.png)\n\n---\n\nGet the number of patients per gender\n\n```SQL\nSELECT\n    gender,\n    COUNT(*) AS patient_count\nFROM\n    patients\nGROUP BY\n    gender;\n```\n\n![patient gender count](images/patient-gender-count.png)\n\n---\n\nList doctors and their specialty\n\n```SQL\nSELECT\n    d.first_name,\n    d.last_name,\n    s.specialty_name\nFROM\n    doctors AS d\nJOIN\n    specialty AS s ON d.specialty_id = s.specialty_id;\n```\n\n![list doctors and their specialty](images/doctor-and-specialty.png)\n\n---\n\nGet the number of patients admitted per doctor\n\n```SQL\nSELECT\n    d.first_name,\n    d.last_name,\n    COUNT(*) AS patient_count\nFROM\n    doctors AS d\nJOIN\n    admissions AS a ON d.doctor_id = a.doctor_id\nGROUP BY\n    d.first_name, d.last_name;\n```\n\n![patients admitted per doctor](images/patients-admitted-per-doctor.png)\n\n---\n\nGet patient names that have been admitted more than once\n\n```SQL\nSELECT\n    p.first_name,\n    p.last_name\nFROM\n    patients AS p\nWHERE\n    p.patient_id\nIN (\n    SELECT\n        patient_id\n    FROM\n        admissions\n    GROUP BY\n        patient_id\n    HAVING\n        COUNT(*) \u003e 1\n)\n```\n\n![patients admitted more than once](images/patients-admitted-more-than-once.png)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanuel7806%2Fsql-server-hospital-database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanuel7806%2Fsql-server-hospital-database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanuel7806%2Fsql-server-hospital-database/lists"}