{"id":28978276,"url":"https://github.com/apollo-level2-web-dev/postgressql-practice-task","last_synced_at":"2025-10-25T11:45:27.879Z","repository":{"id":300645106,"uuid":"675276324","full_name":"Apollo-Level2-Web-Dev/postgresSQL-practice-task","owner":"Apollo-Level2-Web-Dev","description":null,"archived":false,"fork":false,"pushed_at":"2023-08-06T11:40:36.000Z","size":3,"stargazers_count":3,"open_issues_count":0,"forks_count":3,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-22T21:44:12.594Z","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,"zenodo":null}},"created_at":"2023-08-06T11:39:54.000Z","updated_at":"2024-08-21T17:56:01.000Z","dependencies_parsed_at":"2025-06-22T21:44:14.144Z","dependency_job_id":"96fbaa7a-2f6f-4732-aead-17b812ee765d","html_url":"https://github.com/Apollo-Level2-Web-Dev/postgresSQL-practice-task","commit_stats":null,"previous_names":["apollo-level2-web-dev/postgressql-practice-task"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Apollo-Level2-Web-Dev/postgresSQL-practice-task","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FpostgresSQL-practice-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FpostgresSQL-practice-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FpostgresSQL-practice-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FpostgresSQL-practice-task/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/postgresSQL-practice-task/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2FpostgresSQL-practice-task/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261700922,"owners_count":23196508,"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":"2025-06-24T15:13:24.130Z","updated_at":"2025-10-25T11:45:27.769Z","avatar_url":"https://github.com/Apollo-Level2-Web-Dev.png","language":null,"readme":"# postgresSQL-practice-task\n\nTask 1: SELECT and WHERE Clause\nCreate a table named \"employees\" with columns (emp_id, emp_name, department, salary) and insert the following data:\n\n```sql\nCREATE TABLE employees (\n    emp_id INT PRIMARY KEY,\n    emp_name VARCHAR(50),\n    department VARCHAR(50),\n    salary DECIMAL(10, 2)\n);\n\nINSERT INTO employees (emp_id, emp_name, department, salary)\nVALUES\n    (1, 'John Doe', 'HR', 50000.00),\n    (2, 'Jane Smith', 'IT', 60000.00),\n    (3, 'Michael Johnson', 'Finance', 55000.00),\n    (4, 'Emily Brown', 'HR', 52000.00);\n```\n\nWrite an SQL query to retrieve the names and salaries of employees who work in the \"HR\" department.\n\nExpected Result:\n| emp_name | salary |\n|----------------|-----------|\n| John Doe | 50000.00 |\n| Emily Brown | 52000.00 |\n\nTask 2: Aggregation and HAVING Clause\nCreate a table named \"orders\" with columns (order_id, customer_id, total_amount) and insert the following data:\n\n```sql\nCREATE TABLE orders (\n    order_id INT PRIMARY KEY,\n    customer_id INT,\n    total_amount DECIMAL(10, 2)\n);\n\nINSERT INTO orders (order_id, customer_id, total_amount)\nVALUES\n    (101, 1, 200.00),\n    (102, 2, 300.00),\n    (103, 1, 150.00),\n    (104, 3, 400.00),\n    (105, 2, 250.00);\n```\n\nWrite an SQL query to find the customer IDs and the average total amount of their orders. Display only those customers whose average total amount is greater than or equal to 250.\n\nExpected Result:\n| customer_id | average_total_amount |\n|----------------|----------------------|\n| 2 | 275.00 |\n\nTask 3: JOIN and GROUP BY\nCreate two tables named \"students\" and \"courses\" with columns as follows:\n\n```sql\nCREATE TABLE students (\n    student_id INT PRIMARY KEY,\n    student_name VARCHAR(50),\n    age INT,\n    gender VARCHAR(10)\n);\n\nCREATE TABLE courses (\n    course_id INT PRIMARY KEY,\n    course_name VARCHAR(50),\n    credits INT\n);\n\nINSERT INTO students (student_id, student_name, age, gender)\nVALUES\n    (1, 'Alice', 22, 'Female'),\n    (2, 'Bob', 21, 'Male'),\n    (3, 'Charlie', 23, 'Male');\n\nINSERT INTO courses (course_id, course_name, credits)\nVALUES\n    (101, 'Mathematics', 3),\n    (102, 'History', 4),\n    (103, 'Physics', 3);\n\n-- Enrollment table with student-course relationships\nCREATE TABLE enrollment (\n    enrollment_id INT PRIMARY KEY,\n    student_id INT,\n    course_id INT\n);\n\nINSERT INTO enrollment (enrollment_id, student_id, course_id)\nVALUES\n    (1, 1, 101),\n    (2, 1, 102),\n    (3, 2, 103),\n    (4, 3, 101);\n```\n\nWrite an SQL query to retrieve the student name, course name, and credits for all enrolled courses.\n\nExpected Result:\n| student_name | course_name | credits |\n|----------------|----------------|---------|\n| Alice | Mathematics | 3 |\n| Alice | History | 4 |\n| Bob | Physics | 3 |\n| Charlie | Mathematics | 3 |\n\nTask 4: Multiple Joins and Aggregation\nCreate three tables named \"employees,\" \"departments,\" and \"salaries\" with columns as follows:\n\n```sql\nCREATE TABLE employees (\n    emp_id INT PRIMARY KEY,\n    emp_name VARCHAR(50),\n    department_id INT\n);\n\nCREATE TABLE departments (\n    department_id INT PRIMARY KEY,\n    department_name VARCHAR(50)\n);\n\nCREATE TABLE salaries (\n    emp_id INT,\n    salary DECIMAL(10, 2)\n);\n\nINSERT INTO employees (emp_id, emp_name, department_id)\nVALUES\n    (1, 'John Doe', 1),\n    (2, 'Jane Smith', 2),\n    (3, 'Michael Johnson', 1),\n    (4, 'Emily Brown', 3);\n\nINSERT INTO departments (department_id, department_name)\nVALUES\n    (1, 'HR'),\n    (2, 'IT'),\n    (3, 'Finance');\n\nINSERT INTO salaries (emp_id, salary)\nVALUES\n    (1, 50000.00),\n    (2, 60000.00),\n    (3, 55000.00),\n    (4, 52000.00);\n```\n\nWrite an SQL query to retrieve the department name and the average salary of employees working in each department. Sort the results by the average salary in descending order.\n\nExpected Result:\n| department_name | average_salary |\n|-------------------|----------------|\n| IT | 60000.00 |\n| HR | 52500.00 |\n| Finance | 52000.00 |\n\nTask 5: Aggregation and Grouping\nCreate a table named \"orders\" with columns (order_id, customer_id, order_date, total_amount) and insert the following data:\n\n```sql\nCREATE TABLE orders (\n    order_id INT PRIMARY KEY,\n    customer_id INT,\n    order_date DATE,\n    total_amount DECIMAL(10, 2)\n);\n\nINSERT INTO orders (order_id, customer_id, order_date, total_amount)\nVALUES\n    (101, 1, '2023-01-05', 200.00),\n    (102, 2, '2023-01-06', 300.00),\n    (103, 1, '2023-02-10', 150.00),\n    (104, 3, '2023-02-15', 400.00),\n    (105, 2, '2023-03-20', 250.00);\n```\n\nWrite an SQL query to find the total sales amount for each month, along with the number of orders in that month.\n\nExpected Result:\n| Month | Total_Sales | Num_Orders |\n|----------|-------------|------------|\n| January | 550.00 | 2 |\n| February | 550.00 | 2 |\n| March | 250.00 | 1 |\n\nTask 6: Using JOINs and Aggregation\nCreate two tables named \"employees\" and \"salaries\" with columns as follows:\n\n```sql\nCREATE TABLE employees (\n    emp_id INT PRIMARY KEY,\n    emp_name VARCHAR(50),\n    department_id INT\n);\n\nCREATE TABLE salaries (\n    emp_id INT,\n    salary DECIMAL(10, 2)\n);\n\nINSERT INTO employees (emp_id, emp_name, department_id)\nVALUES\n    (1, 'John Doe', 1),\n    (2, 'Jane Smith', 2),\n    (3, 'Michael Johnson', 1),\n    (4, 'Emily Brown', 3);\n\nINSERT INTO salaries (emp_id, salary)\nVALUES\n    (1, 50000.00),\n    (2, 60000.00),\n    (3, 55000.00),\n    (4, 52000.00);\n```\n\nWrite an SQL query to retrieve the department name and the average salary of employees in each department, excluding departments with fewer than two employees.\n\nExpected Result:\n| department_id | avg_salary |\n|-----------------|--------------|\n| 1 | 52500.00 |\n\nTask 7: Using HAVING with Aggregation\nCreate a table named \"products\" with columns (product_id, product_name, stock_quantity) and insert the following data:\n\n```sql\nCREATE TABLE products (\n    product_id INT PRIMARY KEY,\n    product_name VARCHAR(50),\n    stock_quantity INT\n);\n\nINSERT INTO products (product_id, product_name, stock_quantity)\nVALUES\n    (101, 'Widget A', 20),\n    (102, 'Widget B', 10),\n    (103, 'Widget C', 15),\n    (104, 'Widget D', 5);\n```\n\nWrite an SQL query to find the product names and their total sales quantity for products with a total sales quantity greater than 5.\n\nExpected Result:\n| product_name | total_sales_quantity |\n|--------------|---------------------|\n| Widget A | 20 |\n| Widget C | 15 |\n\nTask 8: Combining Multiple Joins\nCreate three tables named \"customers,\" \"orders,\" and \"order_items\" with columns as follows:\n\n```sql\nCREATE TABLE customers (\n    customer_id INT PRIMARY KEY,\n    customer_name VARCHAR(50),\n    city VARCHAR(50)\n);\n\nCREATE TABLE orders (\n    order_id INT PRIMARY KEY,\n    customer_id INT,\n    order_date DATE\n);\n\nCREATE TABLE order_items (\n    item_id INT PRIMARY KEY,\n    order_id INT,\n    product_name VARCHAR(50),\n    quantity INT\n);\n\nINSERT INTO customers (customer_id, customer_name, city)\nVALUES\n    (1, 'John Doe', 'New York'),\n    (2, 'Jane Smith', 'Los Angeles'),\n    (3, 'Michael Johnson', 'Chicago');\n\nINSERT INTO orders (order_id, customer_id, order_date)\nVALUES\n    (101, 1, '2023-01-05'),\n    (102, 2, '2023-02-10'),\n    (103, 1, '2023-02-15');\n\nINSERT INTO order_items (item_id, order_id, product_name, quantity)\nVALUES\n    (1, 101, 'Widget A', 2),\n    (2, 101, 'Widget B', 3),\n    (3, 102, 'Widget C', 1),\n    (4, 103, 'Widget A', 4);\n```\n\nWrite an SQL query to retrieve the customer name, order date, and the total quantity of items ordered for each order.\n\nExpected Result:\n| customer_name | order_date | total_quantity |\n|----------------|--------------|----------------|\n| John Doe | 2023-01-05 | 5 |\n| Jane Smith | 2023-02-10 | 1 |\n| John Doe | 2023-02-15 | 4 |\n\nTask 9: Conditional Aggregation\nCreate a table named \"sales\" with columns (sale_id, product_id, sale_date, sale_amount) and insert the following data:\n\n```sql\nCREATE TABLE sales (\n    sale_id INT PRIMARY KEY,\n    product_id INT,\n    sale_date DATE,\n    sale_amount DECIMAL(10, 2)\n);\n\nINSERT INTO sales (sale_id, product_id, sale_date, sale_amount)\nVALUES\n    (1, 101, '2023-01-05', 200.00),\n    (2, 102, '2023-01-06', 300.00),\n    (3, 101, '2023-02-10', 150.00),\n    (4, 103, '2023-02-15', 400.00),\n    (5, 102, '2023-03-20', 250.00);\n```\n\nWrite an SQL query to find the total sales amount for each product. However, if the total sales amount is less than 300, show it as 0.\n\nExpected Result:\n| product_id | total_sales_amount |\n|--------------|--------------------|\n| 101 | 350.00 |\n| 102 | 550.00 |\n| 103 | 400.00 |\n\nTask 10: Combining Multiple Aggregates\nCreate a table named \"students\" with columns (student_id, student_name, age) and insert the following data:\n\n```sql\nCREATE TABLE students (\n    student_id INT PRIMARY KEY,\n    student_name VARCHAR(50),\n    age INT\n);\n\nINSERT INTO students (student_id, student_name, age)\nVALUES\n    (1, 'Alice', 22),\n    (2, 'Bob', 21),\n    (3, 'Charlie', 23);\n```\n\nWrite an SQL query to find the minimum, maximum, and average age of all students.\n\nExpected Result\n\n:\n| min_age | max_age | avg_age |\n|---------|---------|---------|\n| 21 | 23 | 22.00 |\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fpostgressql-practice-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollo-level2-web-dev%2Fpostgressql-practice-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fpostgressql-practice-task/lists"}