| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This repository contains a collection of PostgreSQL commands for various database management tasks.
This repository provides a set of PostgreSQL commands and examples for managing databases, tables, and data. Each section covers a specific aspect of database management, and the commands are presented with explanations for better understanding.
Grant the necessary permissions to users for performing specific actions within the database.
GRANT CONNECT ON DATABASE test TO postgres;
ALTER USER postgres CREATEDB;
CREATE DATABASE test2;
ALTER DATABASE test2 RENAME TO test3;
DROP DATABASE test3;
CREATE TABLE student (
student_id INT,
first_name VARCHAR(15),
last_name VARCHAR(15),
cgpa NUMERIC(1, 2) );
ALTER TABLE student RENAME TO learner;
DROP TABLE learner;
Create a "users" table with columns for user information and constraints:
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
age INT DEFAULT 18,
UNIQUE(username, email));
Remove all data from the "users" table while keeping its structure:
TRUNCATE TABLE users;
Insert user data into the "users" table using various methods:
Method 1:
INSERT INTO users(user_id, username, email, age)
VALUES ('sam', 'sam@gmail.com', 21);
Method 2:
INSERT INTO users(username, email, age)
VALUES ('clark', 'clark@gmail.com', 35),
('clark2', 'clark2@gmail.com', 35);
Method 3:
INSERT INTO users
VALUES (1, 'persian', 'persian@gmail.com'),
(2, 'persian2', 'persian2@gmail.com');
Add a "password" column to the "users" table with a default value:
ALTER TABLE users
ADD COLUMN password VARCHAR(255) DEFAULT 'admin123' NOT NULL;
Delete the "age" column from the "users" table:
ALTER TABLE users DROP COLUMN age;
Add and modify columns in the "users" table:
ALTER TABLE users ADD COLUMN demo INT;
ALTER TABLE users ALTER COLUMN demo TYPE TEXT;
ALTER TABLE users ALTER COLUMN country SET DEFAULT 'bangladesh';
ALTER TABLE users ALTER COLUMN demo DROP DEFAULT;
Insert a user with specific values into the "users" table:
INSERT INTO users VALUES(4, 'john1', 'john1@gmail.com');
Rename the "demo" column to "country" in the "users" table:
ALTER TABLE users RENAME COLUMN demo TO country;
ALTER TABLE users ALTER COLUMN country SET NOT NULL;
Drop the "NOT NULL" constraint from the "country" column:
ALTER TABLE users ALTER COLUMN country DROP NOT NULL;
Add a unique constraint to the "email" column in the "users" table:
ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE(email);
Delete the unique constraint from the "email" column in the "users" table:
ALTER TABLE users DROP CONSTRAINT unique_email;
SELECT * FROM users;
CREATE TABLE Employees (
empID SERIAL PRIMARY KEY,
empName VARCHAR(50) NOT NULL,
departmentID INT,
CONSTRAINT fk_constraint_dept FOREIGN KEY (departmentID) REFERENCES Department(deptID)
);
Explanation: Create a table to store employee details with a foreign key constraint.
SELECT empid, name, email, joining_date, salary, from employees;
SELECT *
FROM employees
WHERE
name <> 'Eve'
AND salary > 9000
OR name = 'Greta'
UPDATE courses
SET
course_name = 'laravel',
description = 'super'
WHERE course_id = 1;
Explanation: Update course data where course_id is 1.
UPDATE courses
SET
course_name = 'laravel',
description = 'super'
WHERE course_id > 1;
Explanation: Update course data for courses with course_id greater than 1.
DELETE FROM courses WHERE course_id = 1;
Explanation: Delete course data where course_id is 1.
CREATE TABLE IF NOT EXISTS employees (
empID SERIAL PRIMARY KEY,
name text NOT NULL,
email TEXT NOT NULL,
salary INT not NULL,
joining_data DATE NOT NULL,
deptID INT NOT NULL,
CONSTRAINT fk_deptID FOREIGN KEY (deptID) REFERENCES department(deptID));
Explanation: Create employees table if it doesn't exist.
SELECT empid, name, email, joining_date, salary FROM employees;
Explanation: Retrieve specific columns from the employees table.
SELECT * FROM employees WHERE name LIKE '_r%';
SELECT * FROM employees WHERE name LIKE '__r__';
SELECT * FROM employees WHERE name LIKE 'G%a';
SELECT * FROM employees WHERE deptid IS NULL;
Explanation: Retrieve rows based on specified conditions.
SELECT * FROM employees ORDER BY name ASC LIMIT 10 OFFSET 2;
Explanation: Retrieve sorted and limited rows from employees table.
SELECT * FROM employees ORDER BY salary DESC LIMIT 1;
Explanation: Retrieve employee with the highest salary.
SELECT * FROM employees WHERE empid NOT IN (2, 3, 5);
SELECT * FROM employees WHERE salary BETWEEN 10000 AND 15000;
SELECT * FROM employees WHERE name LIKE 'A%';
Explanation: Retrieve rows based on various filtering conditions.
SELECT *
FROM employees
INNER JOIN department ON employees.deptid = department.deptid;
Explanation: Retrieve all columns from both tables where the department ID matches in both tables.
SELECT *
FROM employees
LEFT JOIN department ON department.department_id = employees.department_id;
Explanation: Retrieve all rows from the left table and matching rows from the right table.
SELECT *
FROM employees
RIGHT JOIN department ON department.department_id = employees.department_id;
Explanation: Retrieve all rows from the right table and matching rows from the left table.
SELECT *
FROM employees
FULL JOIN department ON department.department_id = employees.department_id;
Explanation: Retrieve all rows when there is a match in either the left or right table.
SELECT * FROM employees CROSS JOIN department;
Explanation: Generate all possible combinations of rows between two tables.
Perform calculations on groups of rows and summarize data
SELECT
d.name,
AVG(e.salary),
SUM(e.salary),
MAX(e.salary)
FROM employees e
FULL JOIN department d ON e.dept = d.dept
GROUP BY d.name
HAVING AVG(e.salary) > 60000;
Explanation: Calculate average, sum, and maximum salary for each department, filtering results using the HAVING clause.
SELECT
d.name,
SUM(salary),
AVG(salary),
MIN(salary),
COUNT(*)
FROM department d
FULL JOIN employees e ON e.dept = d.deptid
GROUP BY d.deptid;
Explanation: Group and aggregate salary data by department, calculating sum, average, minimum, and count.
Use subqueries to perform complex queries within other queries
SELECT
film_id,
title,
rental_rate
FROM film
WHERE rental_rate > 2.98;
Explanation: Retrieve film details where the rental rate is greater than 2.98.
| Back | FazBrowse Home | New Git URL |