{"id":21964583,"url":"https://github.com/apollo-level2-web-dev/b3-psql-conceptual-session","last_synced_at":"2025-03-22T21:24:42.977Z","repository":{"id":259076260,"uuid":"876261700","full_name":"Apollo-Level2-Web-Dev/B3-psql-conceptual-session","owner":"Apollo-Level2-Web-Dev","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-21T17:08:03.000Z","size":3,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-01-28T01:16:49.022Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/Apollo-Level2-Web-Dev.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-21T17:07:18.000Z","updated_at":"2025-01-12T18:28:18.000Z","dependencies_parsed_at":"2024-10-22T08:11:33.287Z","dependency_job_id":null,"html_url":"https://github.com/Apollo-Level2-Web-Dev/B3-psql-conceptual-session","commit_stats":null,"previous_names":["apollo-level2-web-dev/b3-psql-conceptual-session"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FB3-psql-conceptual-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FB3-psql-conceptual-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FB3-psql-conceptual-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FB3-psql-conceptual-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Apollo-Level2-Web-Dev","download_url":"https://codeload.github.com/Apollo-Level2-Web-Dev/B3-psql-conceptual-session/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245021667,"owners_count":20548381,"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","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":[],"created_at":"2024-11-29T12:25:36.589Z","updated_at":"2025-03-22T21:24:42.952Z","avatar_url":"https://github.com/Apollo-Level2-Web-Dev.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# **SQL Concepts and Tasks: Employees and Departments Database (PostgreSQL)**\r\n\r\n## **Introduction**\r\n\r\nIn this session, we will explore fundamental SQL concepts using **PostgreSQL** by creating and manipulating two related tables: **employees** and **departments**. The **employees** table will store information about employees, including their names, hire dates, salaries, and active status. The **departments** table will hold details about various departments within an organization. Through a series of tasks, we will practice SQL operations such as creating tables, inserting data, defining constraints, querying information, and modifying tables. This hands-on approach will enhance your understanding of SQL and its practical applications in database management using PostgreSQL.\r\n\r\n---\r\n\r\n### **Sample Data Table 1: `employees`**\r\n\r\n| employee_id | first_name | last_name | hire_date  | salary   | department_id | is_active |\r\n| ----------- | ---------- | --------- | ---------- | -------- | ------------- | --------- |\r\n| 1           | John       | Doe       | 2021-05-10 | 50000.00 | 1             | TRUE      |\r\n| 2           | Jane       | Smith     | 2022-03-15 | 62000.00 | 2             | TRUE      |\r\n| 3           | Alice      | Johnson   | 2023-01-20 | 55000.00 | 1             | FALSE     |\r\n| 4           | Bob        | Davis     | 2020-11-30 | 72000.00 | 3             | TRUE      |\r\n\r\n---\r\n\r\n### **Sample Data Table 2: `departments`**\r\n\r\n| department_id | department_name | manager_id |\r\n| ------------- | --------------- | ---------- |\r\n| 1             | Human Resources | 1          |\r\n| 2             | Finance         | 2          |\r\n| 3             | Engineering     | 4          |\r\n\r\n---\r\n\r\n### **Task List Based on the Above Tables**\r\n\r\n#### **Task 1: Creating Tables with Multiple Columns and Data Types**\r\n- **Create the `employees` table** with various data types like `VARCHAR`, `DATE`, `BOOLEAN`, and `NUMERIC`.\r\n- **Create the `departments` table** with a `SERIAL` type for the primary key and a `VARCHAR` type for the department name.\r\n- Ensure that the `first_name`, `last_name`, and `department_name` columns are `NOT NULL`.\r\n- Add a `PRIMARY KEY` constraint to `employee_id` and `department_id`.\r\n- Add a `FOREIGN KEY` constraint on `department_id` in the `employees` table that references `department_id` in the `departments` table.\r\n\r\n#### **Task 2: Inserting Data and Checking Constraints**\r\n- Insert sample records (as shown above) into the `employees` and `departments` tables.\r\n- Attempt to insert a record where `first_name` is `NULL` to validate the `NOT NULL` constraint.\r\n- Try to insert a record with a non-existent `department_id` to test the `FOREIGN KEY` constraint.\r\n\r\n#### **Task 3: Querying the Data**\r\n1. **Basic Queries:**\r\n   - Select all active employees.\r\n   - Find all employees in the Finance department.\r\n\r\n2. **Aggregate Functions:**\r\n   - Calculate the total salary for employees in the Engineering department.\r\n   - Determine the average salary for all employees.\r\n\r\n3. **Sorting and Aliasing:**\r\n   - Select employee names and salaries, aliasing `first_name` as `First Name` and `salary` as `Employee Salary`.\r\n   - Sort employees by their hire date in descending order.\r\n\r\n4. **NULL Filtering:**\r\n   - List all departments where the `manager_id` is NULL (if any records exist).\r\n\r\n5. **Using `LIKE` and `BETWEEN`:**\r\n   - Find all employees whose `last_name` starts with \"J\".\r\n   - Get employees hired between `2021-01-01` and `2022-12-31`.\r\n\r\n#### **Task 4: Modifying Tables**\r\n- Use `ALTER TABLE` to add a new column, `phone_number`, to the `employees` table.\r\n- Modify the `salary` column to increase its precision if necessary.\r\n- Drop the `is_active` column from the `employees` table.\r\n\r\n#### **Task 5: JOIN Operations**\r\n- Write a query to join the `employees` and `departments` tables to display the department name for each employee.\r\n- Find all employees and their manager's name (self-join if managers are also employees).\r\n\r\n#### **Task 6: Subqueries**\r\n- Find the highest-paid employee using a subquery.\r\n- Retrieve all employees whose salary is above the average salary.\r\n\r\n#### **Task 7: Views**\r\n- Create a view called `active_employees` that shows only active employees and their departments.\r\n- Select all data from the created view.\r\n\r\n#### **Task 8: Indexing**\r\n- Create an index on the `last_name` column of the `employees` table to speed up search queries.\r\n- Drop the index if needed.\r\n\r\n#### **Task 9: Pagination**\r\n- Write a query to fetch the first 2 employees ordered by `employee_id`, and then implement pagination using `LIMIT` and `OFFSET`.\r\n\r\n---\r\n# B3-psql-conceptual-session\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fb3-psql-conceptual-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollo-level2-web-dev%2Fb3-psql-conceptual-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fb3-psql-conceptual-session/lists"}