main_repo
https://github.com/sarwar-asik/DB-INFO
SQL Database command (CREATE,UPDATE,GET,DELETE,AGGREGATE,JOINING) ::::
DROP DATABASE university_manaagement2;
CREATE DATABASE test1;
ALTER DATABASE test1 RENAME TO test3
CREATE TABLE student (
student_id INT,
first_name VARCHAR(40),
last_name VARCHAR(30),
cgpa NUMERIC(1,2)
)
ALTER TABLE learners RENAME TO student;
DROP TABLE student;
CREATE TABLE
"user1"(
user_id SERIAL,
user_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
age int DEFAULT 18,
);
insert into user1 (1,'abc', '[email protected]',20)
INSERT INTO
"user1" (user_id, user_name, email, age)
VALUES (
2,
'sarwar',
'[email protected]',
24
);
INSERT INTO
user1 (user_name, email)
VALUES (
'muktadir vai',
'[email protected]'
), (
'mohammad',
'[email protected]'
);
alter TABLE user1 DROP COLUMN age;
ALTER TABLE user1 add COLUMN demo int;
ALTER TABLE user1 ALTER COLUMN demo TYPE TEXT;
ALTER TABLE user1 alter column country set DEFAULT 'bangladesh';
insert into user1 values (5,'Imdad','[email protected]');
ALTER TABLE user1 RENAME COLUMN demo to country;
ALTER TABLE user1 alter COLUMN country set NOT NULL;
ALTER TABLE user1 add constraint unique_email UNIQUE(email);
CREATE TABLE
Department(
deptID SERIAL PRIMARY KEY,
deptName VARCHAR(50)
);
INSERT into department VALUES(1,'IT');
DELETE from department WHERE deptID =1 ;
SELECT * from department;
CREATE TABLE
Employee(
empID SERIAL PRIMARY KEY,
empName VARCHAR(50) NOT NULL,
departmentID INT,
CONSTRAINT fk_constraints_dept FOREIGN KEY (departmentID) REFERENCES Department(deptID)
);
INSERT INTO Employee VALUES(1,'KHorshed',1);
SELECT * from employee;
CREATE Table courses (
course_id SERIAL PRIMARY KEY,
course_name VARCHAR(255) NOT NULL,
description VARCHAR(255),
published_date DATE
);
#####- create courses table data ::::
INSERT INTO courses(course_name,description, published_date)
VALUES
('frontend developer','A complete frontend developer','2023-07-13'),
('backend developer','A complete backend developer',NULL),
('complete web developer','A complete web developer','2023-05-10');
UPDATE courses
set
description ='be a smart tech learner'
WHERE course_id > 1 or course_id < 5
;
DELETE FROM courses
WHERE course_id = 4;
CREATE table IF NOT EXISTs department(
deptID SERIAL PRIMARY KEY,
name TEXT not NULL
);
INSERT INTO department (deptname)
VALUES
('Marketing'),
('Finance'),
('Sales');
CREATE TABLE if not exists employee(
empID SERIAL PRIMARY key,
name text not null,
email TEXT NOT NULL,
salary INTEGER NOT NULL,
joining_date date not NULL,
deptID INTEGER not NULL,
constraint fk_deptID
FOREIGN KEY(deptID)
REFERENCES department(deptID)
);
INSERT INTO employee (name, email, salary, joining_date, deptID)
VALUES
('Samuel Harris', '[email protected]', 52000, '2024-02-20', 1),
('Grace Turner', '[email protected]', 54000, '2024-03-12', 2),
('Liam Mitchell', '[email protected]', 61000, '2024-04-25', 3),
('Emma Adams', '[email protected]', 49000, '2024-05-30', 1),
('Logan Cook', '[email protected]', 57000, '2024-06-05', 2),
('Chloe Bennett', '[email protected]', 53000, '2024-07-18', 3);
SELECT * from department;
SELECT name,email,joining_date from employee;
SELECT * from employee
WHERE salary > 55000 and salary < 60000 and name <> 'Olivia';
SELECT * FROM employee
ORDER BY name
ASC;
SELECT * FROM employee
ORDER BY name
DESC LIMIT 5 OFFSET 1;
SELECT * FROM employee ORDER BY salary DESC
LIMIT 1;
SELECT * FROM employee ORDER BY salary DESC
LIMIT 1 OFFSET 2;
SELECT * from employee
WHERE empid NOT IN (2,3,5,10);
SELECT * from employee
WHERE salary BETWEEN 50000 AND 60000;
SELECT * from employee
WHERE name LIKE '%Ja%';
SELECT * from employee
WHERE name LIKE 'J%';
SELECT * from employee
WHERE name LIKE '%s';
SELECT * from employee
WHERE name LIKE '_m%';
SELECT * from employee
WHERE name LIKE '__m%';
SELECT * from employee
WHERE name LIKE 'J%s';
SELECT * from employee WHERE deptid IS NUll;
CREATE Table department2(
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
INSERT INTO department2 (department_id, department_name)
VALUES
(1, 'Executive'),
(2, 'HR'),
(3, 'Sales'),
(4, 'Development'),
(5, 'Support'),
(6, 'Research');
CREATE Table employee2(
employee_id INT PRIMARY KEY,
full_name VARCHAR(200),
department_id INT,
job_role VARCHAR(100),
manager_id INT,
FOREIGN KEY (department_id) REFERENCES department2(department_id)
);
INSERT INTO employee2 (employee_id, full_name, department_id, job_role, manager_id)
VALUES
(1, 'John Doe', 3, 'Sales Manager', NULL),
(2, 'Jane Smith', 2, 'HR Specialist', 1),
(3, 'Michael Johnson', 4, 'Software Developer', 1),
(4, 'Alice Brown', 2, 'HR Assistant', 2),
(5, 'Robert White', 3, 'Sales Representative', 1),
(6, 'Emily Williams', 4, 'Software Engineer', 3),
(7, 'Daniel Lee', 5, 'Technical Support Specialist', 6),
(8, 'Sophia Martinez', 6, 'Research Scientist', NULL),
(9, 'David Johnson', 3, 'Sales Representative', 1),
(10, 'Olivia Brown', 4, 'Software Developer', 3),
(11, 'James Smith', 5, 'Technical Support Specialist', 6),
(12, 'Ella Garcia', 2, 'HR Specialist', 2),
(13, 'Matthew Davis', 4, 'Software Engineer', 3),
(14, 'Ava Wilson', 6, 'Research Scientist', NULL),
(15, 'Liam Mitchell', 3, 'Sales Representative', 5);
SELECT * from department2;
SELECT * from employee2;
SELECT employee2.full_name,employee2.job_role,employee2.manager_id,department2.department_name
FROM employee2
INNER JOIN department2 ON department2.department_id = employee2.department_id ;
SELECT *
FROM employee2
LEFT JOIN department2 ON department2.department_id = employee2.department_id ;
SELECT *
FROM employee2
RIGHT JOIN department2 ON department2.department_id = employee2.department_id ;
SELECT *
FROM employee2
FULL JOIN department2 ON department2.department_id = employee2.department_id ;
SELECT *
from employee
NATURAL JOIN department2;
SELECT * from employee CROSS JOIN department2;
SELECT * from employee;
SELECT AVG(salary) as AverageSalary from employee;
SELECT MAX(salary) as MaximumSalary from employee;
SELECT SUM(salary) as TotalSalary from employee;
SELECT deptid, AVG(salary) from employee GROUP BY deptid;
SELECT d.deptname, avg(e.salary),sum(e.salary) as TOtalSalary,max(e.salary),min(e.salary),count(*) from employee e
FULL JOIN department d on e.deptid = d.deptid
GROUP BY d.deptname ;
or WITH CONDITION
SELECT d ,sum(salary),count(*) from department d
FULL JOIN employee e on e.deptid = d.deptid
GROUP BY d.deptid HAVING sum(e.salary) > 40000