Here is a short practice for working with PostgreSQL:
- Start by launching the “psql” command-line interface for interacting with the PostgreSQL database.
- Create a new database named “mydatabase” by running the following command:
sqlCopy codeCREATE DATABASE mydatabase;
- Connect to the “mydatabase” database by running the following command:
rCopy code\c mydatabase
- Create a new table named “employees” with the following columns:
scssCopy codeid (integer)
name (varchar(50))
age (integer)
salary (numeric)
by running the following SQL command:
sqlCopy codeCREATE TABLE employees (
id integer,
name varchar(50),
age integer,
salary numeric
);
- Insert the following data into the “employees” table:
pythonCopy code1, 'John Doe', 32, 65000
2, 'Jane Doe', 28, 55000
3, 'Jim Smith', 40, 75000
by running the following SQL command:
sqlCopy codeINSERT INTO employees (id, name, age, salary) VALUES
(1, 'John Doe', 32, 65000),
(2, 'Jane Doe', 28, 55000),
(3, 'Jim Smith', 40, 75000);
- Retrieve all employees who are older than 35 years old by running the following SQL command:
sqlCopy codeSELECT * FROM employees WHERE age > 35;
- Update the salary of all employees by 10% by running the following SQL command:
sqlCopy codeUPDATE employees SET salary = salary * 1.1;
- Delete all employees with a salary of less than 60,000 by running the following SQL command:
sqlCopy codeDELETE FROM employees WHERE salary < 60000;
By practicing these steps, you will gain experience working with the basic SQL commands for inserting, retrieving, updating, and deleting data in a PostgreSQL database. Continue to practice these commands and work on more complex examples to improve your skills and become more proficient with PostgreSQL.