📔
Databases
  • Table of contents
  • SQL
    • Getting Started
      • PgAdmin Tool
    • Data Types
      • Arrays
      • Date/Time/Stamps
      • JSON
      • Internal Functions
      • Sequences
      • User Defined Data Types
    • Database
      • Schema
    • Table
      • Aggregation
      • Usefull Functions
      • Combining Tables
      • Constraints
      • Common Table Expression
      • GROUP BY and HAVING
      • OPERATORS
      • ORDER BY and DISTINCT
      • Views
      • WHERE Clause
    • Order of SQL Execution
    • Advance Table
      • Internals
      • Managing Tables
      • Partitioning Tables
      • Pivotal or Crosstab Tables
    • Joins
      • Cross & Natural Joins
      • Full, Multiple & Self Joins
      • Inner Join
      • Left and Right JOIN
    • Functions
      • Cursors
      • PL/pgSQL
      • Stored Procedures
      • Triggers
        • More on Triggers
    • Indexing
      • Custom Indexes
      • GIN Index
      • Indexes
      • SQL
      • Unique Index
    • Summarization
      • SubQueries
      • Window
  • MongoDB
    • Mongo Administration
    • MongoDB Aggregation
    • MQL
  • Redis
  • Cassandra
    • CQL
    • Data Modelling
      • Advance Data Modelling
    • Cassandra Administration
    • Cassandra Features
Powered by GitBook
On this page
  • Logical
  • Pattern Matching
  • Tips
  • Fetch
  • IS NULL or IS NOT NULL

Was this helpful?

  1. SQL
  2. Table

OPERATORS

bodmas 3000

Logical

SELECT 1=1, 1<1, 1>1, 1<=1, 1>=1;

 ?column? | ?column? | ?column? | ?column? | ?column? 
----------+----------+----------+----------+----------
 t        | f        | f        | t        | t

SELECT 1<>1; 
 ?column? 
----------
 f
 
SELECT 1 = 1 or 1 = 2 ;
 ?column? 
----------
 t

select 1 / 0;
-- ERROR:  division by zero

Pattern Matching

SELECT * FROM actors WHERE last_name LIKE '%son%';

  first_name |  last_name  | gender |    date_of_birth    
------------+-------------+--------+---------------------
 Woody      | Harrelson   | M      | 1961-07-23
 Samuel     | Jackson     | M      | 1948-12-21
 Lina       | Leandersson | F      | 1995-09-27
 Jack       | Nicholson   | M      | 1937-04-22
 Mykelti    | Williamson  | M      | 1957-03-04
 Luke       | Wilson      | M      | 1971-09-21
 Owen       | Wilson      | M      | 1968-11-18
 Patrick    | Wilson      | M      | 1973-07-03

-- ILIKE
-- to ignore the case
SELECT * FROM actors WHERE last_name ILIKE '__i%'; 

 first_name | last_name | gender |    date_of_birth    
------------+-----------+--------+---------------------
 Hiroki     | Doi       | M      | 1999-08-10 
 Alec       | Guiness   | M      | 1914-04-02 
 Rumi       | Hiiragi   | F      | 1987-08-01 
 Miyu       | Irino     | M      | 1988-02-19 
 Keira      | Knightley | F      | 1985-03-26 
 Vivien     | Leigh     | F      | 1913-11-05 
 Yasmin     | Paige     | F      | 1991-06-24 
 Tilda      | Swinton   | F      | 1960-11-05 
 Robin      | Wright    | F      | 1966-04-08 

select 'hello' like 'hello';
select 'hello' like 'h%';
select 'hello' like '%e%';
select 'hello' like '%lo';
select 'hello' like '_ello';
select 'hello' like '__llo';
select 'hello' like '%ll_';

 ?column? | ?column? | ?column? | ?column? | ?column? | ?column? | ?column? 
----------+----------+----------+----------+----------+----------+----------
 t        | t        | t        | t        | t        | t        | t


-- like with length of characters
select * from table where name like '____';

Tips

  • When using AND and OR in sample SQL statement, use brackets to differentiate between statements

  • AND operator is processed before OR operator

  • SQL treats AND operator like multiplication and OR like divide

Fetch

-- OFFSET start { ROW | ROWS }
-- FETCH { FIRST | NEXT } { ROW_COUNT } { ROWS|ROW } ONLY

SELECT
    *
FROM movies
fetch first row only ;

 movie_id |     movie_name     | movie_length | movie_lang | release_date | age_certificate | director_id 
----------+--------------------+--------------+------------+--------------+-----------------+-------------
        1 | A Clockwork Orange |          112 | English    | 1972-02-02   | 18              |          13


SELECT
    *
FROM movies
offset 3
fetch next 10 row only ;

 movie_id |          movie_name           | movie_length | movie_lang | release_date | age_certificate | director_id 
----------+-------------------------------+--------------+------------+--------------+-----------------+-------------
        4 | Blade Runner                  |          121 | English    | 1982-06-25   | 15              |          27
        5 | Chungking Express             |          113 | Chinese    | 1996-08-03   | 15              |          35
        6 | City of God                   |          145 | Portuguese | 2003-01-17   | 18              |          20
        7 | City of Men                   |          140 | Portuguese | 2008-02-29   | 15              |          22
        8 | Cold Fish                     |          108 | Japanese   | 2010-09-12   | 18              |          30
        9 | Crouching Tiger Hidden Dragon |          139 | Chinese    | 2000-07-06   | 12              |          15
       10 | Eyes Wide Shut                |          130 | English    | 1999-07-16   | 18              |          13
       11 | Forrest Gump                  |          119 | English    | 1994-07-06   | PG              |          36
       12 | Gladiator                     |          165 | English    | 2000-05-05   | 15              |          27
       13 | Gone with the Wind            |          123 | English    | 1939-12-15   | PG              |           8

IS NULL or IS NOT NULL

select * from actors where date_of_birth is null;

 first_name | last_name | gender | date_of_birth 
------------+-----------+--------+---------------
 Xian       | Gao       | M      | 
PreviousGROUP BY and HAVINGNextORDER BY and DISTINCT

Last updated 3 years ago

Was this helpful?