Skip to content

Tried to Solve The Practice Problems In Complex Way #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions FunctionInpostgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Create a function that takes a student's score and returns a grade (e.g., A, B, C, F)
CREATE or replace FUNCTION grab_grade(score numeric)
RETURNS TEXT
LANGUAGE plpgsql
as
$$

BEGIN
IF score >= 90 THEN
RETURN 'A';
ELSIF score >= 80 THEN
RETURN 'B';
ELSIF score >= 70 THEN
RETURN 'C';
ELSE
RETURN 'F';
END IF;
END

$$

SELECT name, score, grab_grade(score) FROM students;

select * from students

select * from departments


-- Create a function that returns the full name and department of a student by ID.


SELECT students.name as student_name, departments.name as department_name
FROM students
JOIN departments ON departments.id = students.department_id;

CREATE or replace FUNCTION dpt_fn()
RETURNS TABLE(student_name TEXT, department_name TEXT)
LANGUAGE sql
AS
$$
SELECT students.name as student_name, departments.name as department_name FROM students
JOIN departments ON departments.id = students.department_id;
$$

SELECT * FROM dpt_fn();
114 changes: 114 additions & 0 deletions dateAndGroupingTask.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
CREATE TABLE departments (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL
);

CREATE TABLE students (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INTEGER NOT NULL,
score NUMERIC(10,4) ,
department_id INTEGER NOT NULL REFERENCES departments(id)
);


CREATE TABLE course_enrollment (
id SERIAL PRIMARY KEY,
student_id INTEGER NOT NULL REFERENCES students(id),
course_title VARCHAR(100) NOT NULL,
enroll_on DATE NOT NULL
);

INSERT INTO departments (name)
VALUES
('CSE'),
('EEE'),
('BBA'),
('ME'),
('Civil');


INSERT INTO students (name, age, score, department_id)
VALUES
('Alice', 20, 91.5, 1),
('Bob', 21, 78.0, 2),
('Charlie', 22, 84.3, 1),
('David', 20, 65.7, 3),
('Eve', 23, 88.9, 2),
('Frank', 24, 92.4, 4),
('Grace', 21, NULL, 5),
('Rachel', 22, 85.2, 1),
('Steve', 24, 60.0, 3),
('Trudy', 20, NULL, 2), -- score is NULL
('Uma', 21, 92.7, 1),
('Victor', 23, 48.5, 4),
('Wendy', 25, 75.3, 5),
('Xander', 26, 88.8, 3),
('Yasmine', 22, 79.5, 2),
('Zane', 24, NULL, 4), -- score is NULL
('Abigail', 20, 66.6, 5),
('Heidi', 22, 79.2, 1),
('Ivan', 20, 55.0, 3),
('Judy', 25, 89.5, 2),
('Kevin', 23, 95.0, 1),
('Laura', 24, 70.0, 4),
('Mallory', 21, 67.8, 5),
('Nathan', 22, 82.6, 2),
('Olivia', 20, NULL, 3), -- Another NULL score
('Peter', 26, 74.1, 4),
('Quinn', 23, 90.3, 1);

INSERT INTO course_enrollment (student_id, course_title, enroll_on)
VALUES
(1, 'Database Systems', '2024-01-15'),
(1, 'Operating Systems', '2024-02-10'),
(2, 'Circuit Analysis', '2024-03-12'),
(3, 'Algorithms', '2024-04-01'),
(4, 'Marketing Basics', '2024-02-20'),
(6, 'Thermodynamics', '2024-05-01'),
(7, 'Cybersecurity Fundamentals', '2023-11-15'),
(8, 'Data Analytics', '2024-01-20'),
(9, 'Digital Marketing', '2023-12-05'),
(10, 'Machine Learning', '2024-03-10'),
(11, 'Thermal Engineering', '2024-04-18'),
(12, 'Fluid Mechanics', '2023-10-08'),
(13, 'Econometrics', '2024-05-21'),
(14, 'Intro to AI', '2024-02-28'),
(15, 'Project Management', '2024-03-02'),
(16, 'Database Systems', '2024-04-25'),
(17, 'Software Engineering', CURRENT_DATE);

drop Table course_enrollment;
drop Table students;
drop Table departments;

-- Query Practice & Subqueries (Based on 10-1 to 10-3)
-- Retrieve all students who scored higher than the average score.
-- SELECT avg(score) from students;
SELECT* from students
WHERE score > (SELECT avg(score) from students);

-- Find students whose age is greater than the average age of all students.
-- SELECT avg(age) from students;
SELECT * FROM students
WHERE age > (SELECT avg(age) from students);

-- Get names of students who are enrolled in any course (use IN with subquery).

SELECT * from students;
SELECT * from students
WHERE id IN (SELECT student_id FROM course_enrollment);

-- Retrieve departments with at least one student scoring above 90 (use EXISTS).

SELECT * FROM departments
WHERE EXISTS (
SELECT 1
FROM students
WHERE students.department_id = departments.id
AND students.score > 90
);

SELECT* from students;
SELECT* from departments;
SELECT* from course_enrollment
105 changes: 105 additions & 0 deletions functionProcedures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

/*
@Procedural Approach:
1. Language Support: Supports procedural languages like PL/pgSQL, PL/Perl, PL/Python, etc.
2. Complex Logic: Allows for complex logic using control structures like loops, conditionals, and exception handling.
3. Variable Support: Supports variable declarations and manipulation within the procedural code.
4. Stored Procedures/Functions: Provides the ability to create stored procedures or functions,

@Non-Procedural Approach:
1. Declarative Queries: Focuses on writing declarative SQL queries to retrieve, insert, update, or delete data from the database.
2. Simplicity: Emphasizes simplicity by expressing operations in terms of what data is needed.
3. SQL Functions: Supports SQL functions, which are single SQL statements that return a value or set of values.
4. Performance: Can sometimes offer better performance for simple operations due to the optimized query execution plans generated by the database engine.
*/

SELECT * from employees;

SELECT count(*) FROM employees

CREATE Function emp_count()
RETURNS INT
LANGUAGE sql -- here can be plpgsql/plperl/PL/Python or etc for PROCEDURAL
AS
$$
-- here will be the function body
SELECT count(*) FROM employees
$$

SELECT emp_count();

CREATE or REPLACE function delete_emp()
RETURNS void
LANGUAGE SQL
AS
$$
DELETE FROM employees WHERE employee_id = 30;
$$

SELECT delete_emp();


CREATE OR replace FUNCTION delete_emp_by_id(p_emp_id INT)
RETURNS void
LANGUAGE SQL
AS
$$
DELETE FROM employees WHERE employee_id = p_emp_id;
$$

SELECT delete_emp_by_id(29);

CREATE PROCEDURE remove_emp()
LANGUAGE plpgsql
AS
$$
BEGIN
-- here we can write multiple sql queries or one single queries
-- here will exist the works/action that we want to do using procedure.
DELETE FROM employees WHERE employee_id = 28;
END
$$

CALL remove_emp()

CREATE PROCEDURE remove_emp_var()
LANGUAGE plpgsql
AS
$$
DECLARE
test_var INT;
BEGIN
SELECT employee_id INTO test_var FROM employees WHERE employee_id = 26;
DELETE FROM employees WHERE employee_id = test_var;
END
$$

CALL remove_emp_var()

SELECT * FROM employees



CREATE PROCEDURE remove_emp_by_p(p_employee_id int)
LANGUAGE plpgsql
AS
$$

DECLARE
test_var INT;
-- variable declared.

BEGIN
SELECT employee_id INTO test_var FROM employees WHERE employee_id = p_employee_id;
-- we are setting the id to the variable test_var and then we are doing the operation
DELETE FROM employees WHERE employee_id = test_var;

RAISE NOTICE 'Employee Removed Successfully';
-- this will give a notice if deleted.
END

$$;

CALL remove_emp_by_p(24)

SELECT * FROM employees
17 changes: 17 additions & 0 deletions indexingInPostgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Active: 1747459510114@@localhost@5432@ph_2
CREATE INDEX idx_score ON students(score);

CREATE INDEX idx_enrollment_composite ON course_enrollment(student_id, enroll_on);

SELECT * FROM course_enrollment


EXPLAIN ANALYZE
SELECT *
FROM students
WHERE score > 80;

EXPLAIN ANALYZE
SELECT *
FROM course_enrollment
WHERE student_id = 3 AND enroll_on > '2024-01-01';
Loading