@@ -712,29 +712,184 @@ test=# SELECT make,SUM(price) FROM car GROUP BY make limit 5;
712
712
```
713
713
714
714
** 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
+ ```
716
726
** 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)
717
737
738
+ ```
718
739
** 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
+ ```
720
751
** 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
+
721
777
778
+ ```
722
779
** NULLIF**
723
-
780
+ ```
781
+ test=# SELECT COALESCE(10 / NULLIF(0,0),0);
782
+ coalesce
783
+ ----------
784
+ 0
785
+ (1 row)
786
+ ```
724
787
** Timestamps And Dates Course**
788
+ ```
789
+ test=# SELECT NOW();
790
+ now
791
+ -------------------------------
792
+ 2020-08-22 10:47:28.137981+00
793
+ (1 row)
725
794
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
+ ```
726
807
** 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)
727
814
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
+ ```
728
841
** Extracting Fields From Timestamp**
842
+ ```
843
+ test=# SELECT EXTRACT(YEAR FROM NOW());
844
+ date_part
845
+ -----------
846
+ 2020
847
+ (1 row)
729
848
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)
731
860
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
+ ```
732
881
** What Are Primary Keys**
733
882
734
883
** Understanding Primary Keys**
735
884
885
+ ** DROP Primary Key constrints**
886
+ ```
887
+ ALTER TABLE person DROP CONSTRAINT person_pkey;
888
+ ```
736
889
** Adding Primary Key**
737
-
890
+ ```
891
+ ALTER TABLE person ADD PRIMARY KEY(id);
892
+ ```
738
893
** Unique Constraints**
739
894
740
895
** Check Constraints**
0 commit comments