- Postgres uses SQL
- Database is place where you can store, Manipulate, and retrieve data
- postgres is actual database engine and SQL is language
- SQL is programming language help to interact with database
- SQL help to manage data held in relational database
example :
SELECT * FROM TABLE_NAME- Data is stored in tables
- It is called as relational database because it can have relation between multiple tables
- PostGres is Object-Relational Database management system which allows us manage database
- There are multiple way to connect to pg
- Client
- GUI
- Terminal
- Application
CREATE DATABASE <NAME>: Use create database\lto list all the databasepsql -h localhost -U username -p 5432 <db_name>use this command to connect ot database\c <db_name>from inside psql : to change the dbDROP DATABASE <db_name>: Delete the database
CREATE TABLE <table_name> (
Column Name + data type + constrain
)example:
CREATE TABLE person (
id int,
first_name VARCHAR(50),
last_name VARCHAR(50),
)
-
There is vast list of supported datatype
-
\dto list all the tables -
\d <table_name>describe the table
CREATE TABLE person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
)DROP TABLE <table_name>Delete the table
TODO: ADD Constrain without deleting the table
INSERT INTO <table_name>(
first_name,
last_name
) VALUES ('Deep' , 'Onkar');- each database can have multiple tables
- multiple constrain is added in serial ways
INSERT INTO <table_name> (
first_name,
last_name,
date_of_birth
) VALUES ('Deep', 'Onkar' , DATE '2000-02-20');
- use
mockaroo.comto generate the realistic data \i <file_name>to execute command from file
SELECT * FROM <table_name>
- here
*is to select rows
order byby default sort by asc order- use
DESCto sort in descending order
SELECT * FROM <table_name> ORDER BY <row_name> ASCSELECT DISTINCT <row_name> FROM <table_name>;- allows use to filter data using some condition
SELECT * FROM <table_name> WHERE <condition>=comparator in case of where clause>or<or<=or>=are other comparator operators<>not equal operator
- if you want to limit number of records you get use
LIMITkeyword
SELECT * FROM <table_name> LIMIT 10;- if you want to ignore certain number use
OFFSETkeyword FETCHis same asLIMITonly difference is thatFETCHis official term to get certain rows in sql
SELECT * FROM <table_name> WHERE <column> IN ('' , '' , ...);- use to find data in range
SELECT * FROM <table_name> WHERE <column> BETWEEN DATE '2020-01-01' AND `2021-01-01`- use to filter data based on pattern
- ILIKE ignore the casing issue
SELECT * FROM <table_name> WHERE <column> LIKE <pattern>- allow us to group data based on column
SELECT <column>, COUNT(*) FROM <table_name> GROUP BY <column>SELECT <column>, COUNT(*) FROM <table_name> GROUP BY <column> HAVING COUNT(*) > 10;- function we have seen are the aggregation functions
SELECT MAX(<column> or <expression>) FROM <table_name>SELECT SUM(<column> or <expression>) FROM <table_name>-,+,%,^!factorial
- use to override or name any column
SELECT <column_name>, (price * 0.5) AS discount FROM <table_name>;
- allow you to have default value if something is going to be null
-- Example
SELECT COALESCE(alternative_email , email , "NO EMAIL PROVIDED") AS Email from person;SELECT NOW()will give the current timestampINTERVALcan use to add or sub date
2:26