Skip to content

Commit 10bc508

Browse files
committed
Updated on 22-08-2020
1 parent ed89a42 commit 10bc508

File tree

8 files changed

+1163
-2014
lines changed

8 files changed

+1163
-2014
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ for more postgresql commands check [here](./postgresql-commands.md)
4040
- **Port** `5432`
4141
- **Username** as `POSTGRES_USER`, by default: `postgres`
4242
- **Password** as `POSTGRES_PASSWORD`, by default `passw0rd`
43+
![add_server_to_pgadmin1](./images/add_server_to_pgadmin-1.png)
44+
![add_server_to_pgadmin2](./images/add_server_to_pgadmin.png)
45+
![add_server_to_pgadmin3](./images/add_server_to_pgadmin-3.png)

SQL/person-old.sql

Lines changed: 0 additions & 1009 deletions
This file was deleted.

SQL/person.sql

Lines changed: 1000 additions & 1000 deletions
Large diffs are not rendered by default.

images/add_server_to_pgadmin-1.png

113 KB
Loading

images/add_server_to_pgadmin-3.png

83.1 KB
Loading

images/add_server_to_pgadmin.png

117 KB
Loading

images/mock_data_person.png

-2.77 KB
Loading

postgresql-commands.md

Lines changed: 160 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -712,29 +712,184 @@ test=# SELECT make,SUM(price) FROM car GROUP BY make limit 5;
712712
```
713713

714714
**Basics of Arithmetic Operators**
715-
715+
```
716+
test=# SELECT id,make,model,price, ROUND(price * .10,2) from car limit 5;
717+
id | make | model | price | round
718+
----+-----------+---------------+----------+---------
719+
1 | Honda | CR-V | 54286.10 | 5428.61
720+
2 | Maybach | 57 | 74847.63 | 7484.76
721+
3 | Chevrolet | Suburban 1500 | 62678.03 | 6267.80
722+
4 | Volvo | S80 | 32832.14 | 3283.21
723+
5 | Hyundai | Sonata | 39201.07 | 3920.11
724+
(5 rows)
725+
```
716726
**Arithmetic Operators (ROUND)**
727+
```
728+
test=# SELECT id,make,model,price, ROUND(price * .10,2) AS "10% of price", ROUND(price - (price * .10),2) AS "Rest Amt after 10%" from car limit 5;
729+
id | make | model | price | 10% of price | Rest Amt after 10%
730+
----+-----------+---------------+----------+--------------+--------------------
731+
1 | Honda | CR-V | 54286.10 | 5428.61 | 48857.49
732+
2 | Maybach | 57 | 74847.63 | 7484.76 | 67362.87
733+
3 | Chevrolet | Suburban 1500 | 62678.03 | 6267.80 | 56410.23
734+
4 | Volvo | S80 | 32832.14 | 3283.21 | 29548.93
735+
5 | Hyundai | Sonata | 39201.07 | 3920.11 | 35280.96
736+
(5 rows)
717737
738+
```
718739
**Alias**
719-
740+
```
741+
test=# SELECT id AS "ID",make AS "MAKE",model AS "MODEL",price AS "PRICE", ROUND(price * .10,2) AS "10% Discount", ROUND(price - (price * .10),2) AS "Price after Discount" from car limit 5;
742+
ID | MAKE | MODEL | PRICE | 10% Discount | Price after Discount
743+
----+-----------+---------------+----------+--------------+----------------------
744+
1 | Honda | CR-V | 54286.10 | 5428.61 | 48857.49
745+
2 | Maybach | 57 | 74847.63 | 7484.76 | 67362.87
746+
3 | Chevrolet | Suburban 1500 | 62678.03 | 6267.80 | 56410.23
747+
4 | Volvo | S80 | 32832.14 | 3283.21 | 29548.93
748+
5 | Hyundai | Sonata | 39201.07 | 3920.11 | 35280.96
749+
(5 rows
750+
```
720751
**Coalesce**
752+
```
753+
test=# SELECT COALESCE(email,'Email not provided')FROM person;
754+
coalesce
755+
--------------------------------------
756+
757+
758+
759+
760+
761+
762+
Email not provided
763+
764+
765+
Email not provided
766+
767+
Email not provided
768+
769+
770+
Email not provided
771+
772+
Email not provided
773+
Email not provided
774+
Email not provided
775+
776+
721777
778+
```
722779
**NULLIF**
723-
780+
```
781+
test=# SELECT COALESCE(10 / NULLIF(0,0),0);
782+
coalesce
783+
----------
784+
0
785+
(1 row)
786+
```
724787
**Timestamps And Dates Course**
788+
```
789+
test=# SELECT NOW();
790+
now
791+
-------------------------------
792+
2020-08-22 10:47:28.137981+00
793+
(1 row)
725794
795+
test=# SELECT NOW()::DATE;
796+
now
797+
------------
798+
2020-08-22
799+
(1 row)
800+
801+
test=# SELECT NOW()::TIME;
802+
now
803+
-----------------
804+
10:48:04.193382
805+
(1 row)
806+
```
726807
**Adding And Subtracting With Dates**
808+
```
809+
test=# SELECT NOW();
810+
now
811+
-------------------------------
812+
2020-08-22 10:49:46.718122+00
813+
(1 row)
727814
815+
test=# SELECT NOW() - INTERVAL '1 YEAR';
816+
?column?
817+
-------------------------------
818+
2019-08-22 10:50:14.951412+00
819+
(1 row)
820+
821+
test=# SELECT NOW() - INTERVAL '10 YEAR';
822+
?column?
823+
-------------------------------
824+
2010-08-22 10:50:27.188566+00
825+
(1 row)
826+
827+
test=#
828+
test=# SELECT NOW() - INTERVAL '7 MONTHS';
829+
?column?
830+
-------------------------------
831+
2020-01-22 10:51:18.430986+00
832+
(1 row)
833+
834+
test=# SELECT NOW() - INTERVAL '2 DAYS';
835+
?column?
836+
-------------------------------
837+
2020-08-20 10:51:35.083528+00
838+
(1 row)
839+
840+
```
728841
**Extracting Fields From Timestamp**
842+
```
843+
test=# SELECT EXTRACT(YEAR FROM NOW());
844+
date_part
845+
-----------
846+
2020
847+
(1 row)
729848
730-
**Age Function**
849+
test=# SELECT EXTRACT(MONTH FROM NOW());
850+
date_part
851+
-----------
852+
8
853+
(1 row)
854+
855+
test=# SELECT EXTRACT(DAY FROM NOW());
856+
date_part
857+
-----------
858+
22
859+
(1 row)
731860
861+
test=# SELECT EXTRACT(DOW FROM NOW());
862+
date_part
863+
-----------
864+
6
865+
(1 row)
866+
867+
test=#
868+
```
869+
**Age Function**
870+
```
871+
test=# SELECT first_name, last_name, gender, country_of_birth, date_of_birth, AGE(NOW(),date_of_birth) FROM person LIMIT 5;
872+
first_name | last_name | gender | country_of_birth | date_of_birth | age
873+
------------+-----------+--------+------------------+---------------+-----------------------------------------
874+
Feliks | Prandin | Male | Indonesia | 2012-10-18 | 7 years 10 mons 4 days 11:11:37.50254
875+
Gabbie | Scrase | Female | China | 1965-06-02 | 55 years 2 mons 20 days 11:11:37.50254
876+
Alexandra | Ashbe | Female | Ukraine | 1993-12-06 | 26 years 8 mons 16 days 11:11:37.50254
877+
Cheston | Eustanch | Male | Sweden | 1959-10-04 | 60 years 10 mons 18 days 11:11:37.50254
878+
Ruthe | McPartlin | Female | China | 1998-02-25 | 22 years 5 mons 25 days 11:11:37.50254
879+
(5 rows)
880+
```
732881
**What Are Primary Keys**
733882

734883
**Understanding Primary Keys**
735884

885+
**DROP Primary Key constrints**
886+
```
887+
ALTER TABLE person DROP CONSTRAINT person_pkey;
888+
```
736889
**Adding Primary Key**
737-
890+
```
891+
ALTER TABLE person ADD PRIMARY KEY(id);
892+
```
738893
**Unique Constraints**
739894

740895
**Check Constraints**

0 commit comments

Comments
 (0)