SnowConvert: Redshift SELECT Statement¶

SELECT¶

Description¶

Returns rows from tables, views, and user-defined functions. (Redshift SQL Language Reference SELECT statement)

Grammar Syntax¶

 [ WITH with_subquery [, ...] ]
SELECT
[ TOP number | [ ALL | DISTINCT ]
* | expression [ AS output_name ] [, ...] ]
[ FROM table_reference [, ...] ]
[ WHERE condition ]
[ [ START WITH expression ] CONNECT BY expression ]
[ GROUP BY expression [, ...] ]
[ HAVING condition ]
[ QUALIFY condition ]
[ { UNION | ALL | INTERSECT | EXCEPT | MINUS } query ]
[ ORDER BY expression [ ASC | DESC ] ]
[ LIMIT { number | ALL } ]
[ OFFSET start ]
Copy

For more information please refer to each of the following links:

  1. WITH clause

  2. SELECT list

  3. FROM clause

  4. WHERE clause

  5. CONNECT BY clause

  6. GROUP BY clause

  7. HAVING clause

  8. QUALIFY clause

  9. UNION, INTERSECT, and EXCEPT

  10. ORDER BY clause

CONNECT BY clause¶

Description¶

The CONNECT BY clause specifies the relationship between rows in a hierarchy. You can use CONNECT BY to select rows in a hierarchical order by joining the table to itself and processing the hierarchical data. (Redshift SQL Language Reference CONNECT BY Clause)

The CONNECT BY clause is supported in Snowflake.

Grammar Syntax

 [START WITH start_with_conditions]
CONNECT BY connect_by_conditions
Copy

Sample Source Patterns

Input Code:

 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT COUNT(*)
FROM
Employee "start"
CONNECT BY PRIOR id = manager_id
START WITH name = 'John';
Copy

COUNT(*)

12

Output Code:
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT COUNT(*)
FROM
  Employee "start"
CONNECT BY PRIOR id = manager_id
START WITH RTRIM( name) = RTRIM( 'John');
Copy

COUNT(*)

12

There are no known issues.

FROM clause

Description

The FROM clause in a query lists the table references (tables, views, and subqueries) that data is selected from. If multiple table references are listed, the tables must be joined, using appropriate syntax in either the FROM clause or the WHERE clause. If no join criteria are specified, the system processes the query as a cross-join. (Redshift SQL Language Reference FROM Clause)

Warning

The FROM clause is partially supported in Snowflake. Object unpivoting is not currently supported.

Grammar Syntax¶

 FROM table_reference [, ...]

<table_reference> ::=
with_subquery_table_name [ table_alias ]
table_name [ * ] [ table_alias ]
( subquery ) [ table_alias ]
table_reference [ NATURAL ] join_type table_reference
   [ ON join_condition | USING ( join_column [, ...] ) ]
table_reference PIVOT ( 
   aggregate(expr) [ [ AS ] aggregate_alias ]
   FOR column_name IN ( expression [ AS ] in_alias [, ...] )
) [ table_alias ]
table_reference UNPIVOT [ INCLUDE NULLS | EXCLUDE NULLS ] ( 
   value_column_name 
   FOR name_column_name IN ( column_reference [ [ AS ]
   in_alias ] [, ...] )
) [ table_alias ]
UNPIVOT expression AS value_alias [ AT attribute_alias ]
Copy

Sample Source Patterns¶

Join types¶

Snowflake supports all types of joins. For more information, see the JOIN documentation.

Input Code:¶
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

CREATE TABLE department (
    id INT,
    name VARCHAR(50),
    manager_id INT
);

INSERT INTO department(id, name, manager_id) VALUES
(1, 'HR', 100),
(2, 'Sales', 101),
(3, 'Engineering', 102),
(4, 'Marketing', 103);

SELECT e.name AS employee_name, d.name AS department_name
FROM employee e
INNER JOIN department d ON e.manager_id = d.manager_id;

SELECT e.name AS employee_name, d.name AS department_name
FROM employee e
LEFT JOIN department d ON e.manager_id = d.manager_id;

SELECT d.name AS department_name, e.name AS manager_name
FROM department d
RIGHT JOIN employee e ON d.manager_id = e.id;

SELECT e.name AS employee_name, d.name AS department_name
FROM employee e
FULL JOIN department d ON e.manager_id = d.manager_id;
Copy
Inner Join¶
EMPLOYEE_NAMEDEPARTMENT_NAME
JohnHR
JorgeSales
KwakuSales
LiuSales
MateoEngineering
NikkiMarketing
PauloMarketing
RichardMarketing
SofíaEngineering
Left Join¶

EMPLOYEE_NAME

DEPARTMENT_NAME

Carlos

null

John

HR

Jorge

Sales

Kwaku

Sales

Liu

Sales

Mateo

Engineering

Nikki

Marketing

Paulo

Marketing

Richard

Marketing

Saanvi

null

Shirley

null

Sofía

Engineering

Zhang

null

Right Join¶

DEPARTMENT_NAME

MANAGER_NAME

HR

Carlos

Sales

John

Engineering

Jorge

Marketing

Kwaku

null

Liu

null

Mateo

null

Nikki

null

Paulo

null

Richard

null

Saanvi

null

Shirley

null

Sofía

null

Zhang

Full Join¶

EMPLOYEE_NAME

DEPARTMENT_NAME

Carlos

null

John

HR

Jorge

Sales

Kwaku

Sales

Liu

Sales

Mateo

Engineering

Nikki

Marketing

Paulo

Marketing

Richard

Marketing

Saanvi

null

Shirley

null

Sofía

Engineering

Zhang

null

Output Code:¶
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

CREATE TABLE department (
    id INT,
    name VARCHAR(50),
    manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO department (id, name, manager_id) VALUES
(1, 'HR', 100),
(2, 'Sales', 101),
(3, 'Engineering', 102),
(4, 'Marketing', 103);

SELECT e.name AS employee_name, d.name AS department_name
FROM
employee e
INNER JOIN
  department d ON e.manager_id = d.manager_id;

SELECT e.name AS employee_name, d.name AS department_name
FROM
employee e
LEFT JOIN
  department d ON e.manager_id = d.manager_id;

SELECT d.name AS department_name, e.name AS manager_name
FROM
department d
RIGHT JOIN
  employee e ON d.manager_id = e.id;

SELECT e.name AS employee_name, d.name AS department_name
FROM
employee e
FULL JOIN
  department d ON e.manager_id = d.manager_id;
Copy
Inner Join¶

EMPLOYEE_NAME

DEPARTMENT_NAME

John

HR

Jorge

Sales

Kwaku

Sales

Liu

Sales

Mateo

Engineering

Nikki

Marketing

Paulo

Marketing

Richard

Marketing

Sofía

Engineering

Left Join¶

EMPLOYEE_NAME

DEPARTMENT_NAME

Carlos

null

John

HR

Jorge

Sales

Kwaku

Sales

Liu

Sales

Mateo

Engineering

Nikki

Marketing

Paulo

Marketing

Richard

Marketing

Saanvi

null

Shirley

null

Sofía

Engineering

Zhang

null

Right Join¶

DEPARTMENT_NAME

MANAGER_NAME

HR

Carlos

Sales

John

Engineering

Jorge

Marketing

Kwaku

null

Liu

null

Mateo

null

Nikki

null

Paulo

null

Richard

null

Saanvi

null

Shirley

null

Sofía

null

Zhang

Full Join¶

EMPLOYEE_NAME

DEPARTMENT_NAME

Carlos

null

John

HR

Jorge

Sales

Kwaku

Sales

Liu

Sales

Mateo

Engineering

Nikki

Marketing

Paulo

Marketing

Richard

Marketing

Saanvi

null

Shirley

null

Sofía

Engineering

Zhang

null

Pivot Clause¶

Note

Column aliases cannot be used in the IN clause of the PIVOT query in Snowflake.

Input Code:¶
 SELECT *
FROM
    (SELECT e.manager_id, d.name AS department, e.id AS employee_id
     FROM employee e
     JOIN department d ON e.manager_id = d.manager_id) AS SourceTable
PIVOT
    (
     COUNT(employee_id)
     FOR department IN ('HR', 'Sales', 'Engineering', 'Marketing')
    ) AS PivotTable;
Copy

MANAGER_ID

‘HR’

‘Sales’

‘Engineering’

‘Marketing’

100

1

0

0

0

101

0

3

0

0

102

0

0

2

0

103

0

0

0

3

Output Code:¶
 SELECT *
FROM
    (SELECT e.manager_id, d.name AS department, e.id AS employee_id
     FROM
     employee e
     JOIN
         department d ON e.manager_id = d.manager_id) AS SourceTable
PIVOT
    (
     COUNT(employee_id)
     FOR department IN ('HR', 'Sales', 'Engineering', 'Marketing')
    ) AS PivotTable;
Copy

MANAGER_ID

‘HR’

‘Sales’

‘Engineering’

‘Marketing’

100

1

0

0

0

101

0

3

0

0

102

0

0

2

0

103

0

0

0

3

Unpivot Clause¶

Note

Column aliases cannot be used in the IN clause of the UNPIVOT query in Snowflake.

Input Code:¶
 CREATE TABLE count_by_color (quality VARCHAR, red INT, green INT, blue INT);

INSERT INTO count_by_color VALUES ('high', 15, 20, 7);
INSERT INTO count_by_color VALUES ('normal', 35, NULL, 40);
INSERT INTO count_by_color VALUES ('low', 10, 23, NULL);


SELECT *
FROM (SELECT red, green, blue FROM count_by_color) UNPIVOT (
    cnt FOR color IN (red, green, blue)
);

SELECT *
FROM (SELECT red, green, blue FROM count_by_color) UNPIVOT (
    cnt FOR color IN (red r, green as g, blue)
);
Copy

COLOR

CNT

RED

15

RED

35

RED

10

GREEN

20

GREEN

23

BLUE

7

BLUE

40

Output Code:¶
 CREATE TABLE count_by_color (quality VARCHAR, red INT, green INT, blue INT)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO count_by_color
VALUES ('high', 15, 20, 7);
INSERT INTO count_by_color
VALUES ('normal', 35, NULL, 40);
INSERT INTO count_by_color
VALUES ('low', 10, 23, NULL);


SELECT *
FROM (SELECT red, green, blue FROM
            count_by_color
    ) UNPIVOT (
    cnt FOR color IN (red, green, blue)
);

SELECT *
FROM (SELECT red, green, blue FROM
            count_by_color
) UNPIVOT (
    cnt FOR color IN (red
                          !!!RESOLVE EWI!!! /*** SSC-EWI-RS0005 - COLUMN ALIASES CANNOT BE USED IN THE IN CLAUSE OF THE PIVOT/UNPIVOT QUERY IN SNOWFLAKE. ***/!!!
 r, green
          !!!RESOLVE EWI!!! /*** SSC-EWI-RS0005 - COLUMN ALIASES CANNOT BE USED IN THE IN CLAUSE OF THE PIVOT/UNPIVOT QUERY IN SNOWFLAKE. ***/!!!
 as g, blue)
);
Copy

COLOR

CNT

RED

15

GREEN

20

BLUE

7

RED

35

BLUE

40

RED

10

GREEN

23

Related EWIs¶

  1. SSC-EWI-RS0005: Column aliases cannot be used in the IN clause of the PIVOT/UNPIVOT query in Snowflake.

GROUP BY clause¶

Description¶

The GROUP BY clause identifies the grouping columns for the query. Grouping columns must be declared when the query computes aggregates with standard functions such as SUM, AVG, and COUNT. (Redshift SQL Language Reference GROUP BY Clause)

The GROUP BY clause is fully supported in Snowflake.

Grammar Syntax

 GROUP BY group_by_clause [, ...]

group_by_clause := {
    expr |
    GROUPING SETS ( () | group_by_clause [, ...] ) |
    ROLLUP ( expr [, ...] ) |
    CUBE ( expr [, ...] )
    }
Copy

Sample Source Patterns

Grouping sets

Input Code:
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT 
    manager_id,
    COUNT(id) AS total_employees
FROM employee
GROUP BY GROUPING SETS 
    ((manager_id), ())
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

100

1

101

3

102

2

103

3

104

3

null

1

null

13

Output Code:
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT
    manager_id,
    COUNT(id) AS total_employees
FROM
    employee
GROUP BY GROUPING SETS
    ((manager_id), ())
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

100

1

101

3

102

2

103

3

104

3

null

1

null

13

Group by Cube

Input Code:
 SELECT
    manager_id,
    COUNT(id) AS total_employees
FROM
    employee
GROUP BY CUBE(manager_id)
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

100

1

101

3

102

2

103

3

104

3

null

1

null

13

Output Code:
 SELECT
    manager_id,
    COUNT(id) AS total_employees
FROM
    employee
GROUP BY CUBE(manager_id)
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

100

1

101

3

102

2

103

3

104

3

null

1

null

13

Group by Rollup

Input Code:
 SELECT
    manager_id,
    COUNT(id) AS total_employees
FROM
    employee
GROUP BY ROLLUP(manager_id)
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

100

1

101

3

102

2

103

3

104

3

null

1

null

13

Output Code:
 SELECT
    manager_id,
    COUNT(id) AS total_employees
FROM
    employee
GROUP BY ROLLUP(manager_id)
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

100

1

101

3

102

2

103

3

104

3

null

1

null

13

Related EWIs

There are no known issues.

HAVING clause

Description

The HAVING clause applies a condition to the intermediate grouped result set that a query returns. (Redshift SQL Language Reference HAVING Clause)

The HAVING clause is fully supported in Snowflake.

Grammar Syntax¶

 [ HAVING condition ]
Copy

Sample Source Patterns¶

Input Code:¶

 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT manager_id, COUNT(id) AS total_employees
FROM
employee
GROUP BY manager_id
HAVING COUNT(id) > 2
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

101

3

103

3

104

3

Output Code:¶
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT manager_id, COUNT(id) AS total_employees
FROM
employee
GROUP BY manager_id
HAVING COUNT(id) > 2
ORDER BY manager_id;
Copy

MANAGER_ID

TOTAL_EMPLOYEES

101

3

103

3

104

3

Related EWIs¶

There are no known issues.

ORDER BY clause¶

Description¶

The ORDER BY clause sorts the result set of a query. (Redshift SQL Language Reference Order By Clause)

The ORDER BY clause is fully supported in Snowflake.

Grammar Syntax

 [ ORDER BY expression [ ASC | DESC ] ]
[ NULLS FIRST | NULLS LAST ]
[ LIMIT { count | ALL } ]
[ OFFSET start ]
Copy

Sample Source Patterns

Input Code:

 CREATE TABLE employee (
    id INT,
    name VARCHAR(20),
    manager_id INT,
    salary DECIMAL(10, 2)
);

INSERT INTO employee (id, name, manager_id, salary) VALUES
(100, 'Carlos', NULL, 120000.00),
(101, 'John', 100, 90000.00),
(102, 'Jorge', 101, 95000.00),
(103, 'Kwaku', 101, 105000.00),
(104, 'Paulo', 102, 110000.00),
(105, 'Richard', 102, 85000.00),
(106, 'Mateo', 103, 95000.00),
(107, 'Liu', 103, 108000.00),
(108, 'Zhang', 104, 95000.00);

SELECT id, name, manager_id, salary
FROM employee
ORDER BY salary DESC NULLS LAST, name ASC NULLS FIRST
LIMIT 5                                        
OFFSET 2;
Copy

ID

NAME

MANAGER_ID

SALARY

107

Liu

103

108000.00

103

Kwaku

101

105000.00

102

Jorge

101

95000.00

106

Mateo

103

95000.00

108

Zhang

104

95000.00

Output Code:
 CREATE TABLE employee (
    id INT,
    name VARCHAR(20),
    manager_id INT,
    salary DECIMAL(10, 2)
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id, salary) VALUES
(100, 'Carlos', NULL, 120000.00),
(101, 'John', 100, 90000.00),
(102, 'Jorge', 101, 95000.00),
(103, 'Kwaku', 101, 105000.00),
(104, 'Paulo', 102, 110000.00),
(105, 'Richard', 102, 85000.00),
(106, 'Mateo', 103, 95000.00),
(107, 'Liu', 103, 108000.00),
(108, 'Zhang', 104, 95000.00);

SELECT id, name, manager_id, salary
FROM
    employee
ORDER BY salary DESC NULLS LAST, name ASC NULLS FIRST
LIMIT 5
OFFSET 2;
Copy

ID

NAME

MANAGER_ID

SALARY

107

Liu

103

108000.00

103

Kwaku

101

105000.00

102

Jorge

101

95000.00

106

Mateo

103

95000.00

108

Zhang

104

95000.00

Related EWIs

There are no known issues.

QUALIFY clause

Description

The QUALIFY clause filters results of a previously computed window function according to user‑specified search conditions. You can use the clause to apply filtering conditions to the result of a window function without using a subquery. (Redshift SQL Language Reference QUALIFY Clause)

The QUALIFY clause is supported in Snowflake.

Grammar Syntax¶

 QUALIFY condition
Copy

Sample Source Patterns¶

Input Code:¶

 CREATE TABLE store_sales 
(
    ss_sold_date DATE, 
    ss_sold_time TIME, 
    ss_item TEXT, 
    ss_sales_price FLOAT
);

INSERT INTO store_sales VALUES ('2022-01-01', '09:00:00', 'Product 1', 100.0),
                               ('2022-01-01', '11:00:00', 'Product 2', 500.0),
                               ('2022-01-01', '15:00:00', 'Product 3', 20.0),
                               ('2022-01-01', '17:00:00', 'Product 4', 1000.0),
                               ('2022-01-01', '18:00:00', 'Product 5', 30.0),
                               ('2022-01-02', '10:00:00', 'Product 6', 5000.0),
                               ('2022-01-02', '16:00:00', 'Product 7', 5.0);

SELECT *
FROM store_sales ss
WHERE ss_sold_time > time '12:00:00'
QUALIFY row_number()
OVER (PARTITION BY ss_sold_date ORDER BY ss_sales_price DESC) <= 2;
Copy

SS_SOLD_DATE

SS_SOLD_TIME

SS_ITEM

SS_SALES_PRICE

2022-01-01

17:00:00

Product 4

1000

2022-01-01

18:00:00

Product 5

30

2022-01-02

16:00:00

Product 7

5

Output Code:¶
 CREATE TABLE store_sales
(
    ss_sold_date DATE,
    ss_sold_time TIME,
    ss_item TEXT,
    ss_sales_price FLOAT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO store_sales
VALUES ('2022-01-01', '09:00:00', 'Product 1', 100.0),
                               ('2022-01-01', '11:00:00', 'Product 2', 500.0),
                               ('2022-01-01', '15:00:00', 'Product 3', 20.0),
                               ('2022-01-01', '17:00:00', 'Product 4', 1000.0),
                               ('2022-01-01', '18:00:00', 'Product 5', 30.0),
                               ('2022-01-02', '10:00:00', 'Product 6', 5000.0),
                               ('2022-01-02', '16:00:00', 'Product 7', 5.0);

SELECT *
FROM
    store_sales ss
WHERE ss_sold_time > time '12:00:00'
QUALIFY row_number()
OVER (PARTITION BY ss_sold_date ORDER BY ss_sales_price DESC) <= 2;
Copy

SS_SOLD_DATE

SS_SOLD_TIME

SS_ITEM

SS_SALES_PRICE

2022-01-02

16:00:00

Product 7

5

2022-01-01

17:00:00

Product 4

1000

2022-01-01

18:00:00

Product 5

30

Related EWIs¶

There are no known issues.

SELECT list¶

Description¶

The SELECT list names the columns, functions, and expressions that you want the query to return. The list represents the output of the query. (Redshift SQL Language Reference SELECT list)

The query start options are fully supported in Snowflake. Just keep in mind that in Snowflake the DISTINCT and ALL options must go at the beginning of the query.

Note

In Redshift, if your application allows foreign keys or invalid primary keys, it can cause queries to return incorrect results. For example, a SELECT DISTINCT query could return duplicate rows if the primary key column does not contain all unique values. (Redshift SQL Language Reference SELECT list)

Grammar Syntax¶

 SELECT
[ TOP number ]
[ ALL | DISTINCT ] * | expression [ AS column_alias ] [, ...]
Copy

Sample Source Patterns¶

Top clause¶

Input Code:¶
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);
  
SELECT TOP 5 id, name, manager_id
FROM employee;
Copy

ID

NAME

MANAGER_ID

100

Carlos

null

101

John

100

102

Jorge

101

103

Kwaku

101

110

Liu

101

Output Code:¶
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT TOP 5 id, name, manager_id
FROM
    employee;
Copy

ID

NAME

MANAGER_ID

100

Carlos

null

101

John

100

102

Jorge

101

103

Kwaku

101

110

Liu

101

ALL¶

Input Code:¶
SELECT ALL manager_id
FROM employee;
Copy

MANAGER_ID

null

100

101

101

101

102

103

103

103

104

104

102

104

Output Code:¶
 SELECT ALL manager_id
FROM
    employee;
Copy

MANAGER_ID

null

100

101

101

101

102

103

103

103

104

104

102

104

DISTINCT¶

Input Code:¶
SELECT DISTINCT manager_id
FROM employee;
Copy

MANAGER_ID

null

100

101

102

103

104

Output Code:¶
SELECT DISTINCT manager_id
FROM 
    employee;
Copy

MANAGER_ID

null

100

101

102

103

104

Related EWIs¶

There are no known issues.

UNION, INTERSECT, and EXCEPT¶

Description¶

The UNION, INTERSECT, and EXCEPT set operators are used to compare and merge the results of two separate query expressions. (Redshift SQL Language Reference Set Operators)

Set operators are fully supported in Snowflake.

Grammar Syntax

 query
{ UNION [ ALL ] | INTERSECT | EXCEPT | MINUS }
query
Copy

Sample Source Patterns

Input Code:

 SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 101

UNION

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 102

UNION ALL

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 101

INTERSECT

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 103

EXCEPT

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 104;
Copy

ID

NAME

MANAGER_ID

103

Kwaku

101

110

Liu

101

102

Jorge

101

106

Mateo

102

201

Sofía

102

Output Code:
 SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 101

UNION

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 102

UNION ALL

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 101

INTERSECT

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 103

EXCEPT

SELECT id, name, manager_id
FROM
employee
WHERE manager_id = 104;
Copy

ID

NAME

MANAGER_ID

102

Jorge

101

103

Kwaku

101

110

Liu

101

106

Mateo

102

201

Sofía

102

Related EWIs

There are no known issues.

WHERE clause

Description

The WHERE clause contains conditions that either join tables or apply predicates to columns in tables. (Redshift SQL Language Reference WHERE Clause)

The WHERE clause is fully supported in Snowflake.

Grammar Syntax¶

 [ WHERE condition ]
Copy

Sample Source Patterns¶

Input Code:¶

 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT id, name, manager_id
FROM employee
WHERE name LIKE 'J%';
Copy

ID

NAME

MANAGER_ID

101

John

100

102

Jorge

101

Output Code:¶
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);

SELECT id, name, manager_id
FROM
  employee
WHERE name LIKE 'J%' ESCAPE '\\';
Copy

ID

NAME

MANAGER_ID

101

John

100

102

Jorge

101

Related EWIs¶

There are no known issues.

WITH clause¶

Description¶

A WITH clause is an optional clause that precedes the SELECT list in a query. The WITH clause defines one or more common_table_expressions. Each common table expression (CTE) defines a temporary table, which is similar to a view definition. You can reference these temporary tables in the FROM clause. (Redshift SQL Language Reference WITH Clause)

The WITH clause is fully supported in Snowflake.

Grammar Syntax

 [ WITH [RECURSIVE] common_table_expression [, common_table_expression , ...] ]

--Where common_table_expression can be either non-recursive or recursive. 
--Following is the non-recursive form:
CTE_table_name [ ( column_name [, ...] ) ] AS ( query )

--Following is the recursive form of common_table_expression:
CTE_table_name (column_name [, ...] ) AS ( recursive_query )
Copy

Sample Source Patterns

Recursive form

Input Code:
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
);
  
INSERT INTO employee(id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);
  

WITH RECURSIVE john_org(id, name, manager_id, level) AS
( SELECT id, name, manager_id, 1 AS level
  FROM employee
  WHERE name = 'John'
  UNION ALL
  SELECT e.id, e.name, e.manager_id, level + 1 AS next_level
  FROM employee e, john_org j
  WHERE e.manager_id = j.id and level < 4
)
SELECT DISTINCT id, name, manager_id FROM john_org ORDER BY manager_id;
Copy

ID

NAME

MANAGER_ID

101

John

100

110

Liu

101

102

Jorge

101

103

Kwaku

101

201

Sofía

102

106

Mateo

102

105

Richard

103

104

Paulo

103

110

Nikki

103

205

Zhang

104

120

Saanvi

104

200

Shirley

104

Output Code:
 CREATE TABLE employee (
  id INT,
  name VARCHAR(20),
  manager_id INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "redshift",  "convertedOn": "11/05/2024",  "domain": "test" }}';

INSERT INTO employee (id, name, manager_id) VALUES
(100, 'Carlos', null),
(101, 'John', 100),
(102, 'Jorge', 101),
(103, 'Kwaku', 101),
(110, 'Liu', 101),
(106, 'Mateo', 102),
(110, 'Nikki', 103),
(104, 'Paulo', 103),
(105, 'Richard', 103),
(120, 'Saanvi', 104),
(200, 'Shirley', 104),
(201, 'Sofía', 102),
(205, 'Zhang', 104);


WITH RECURSIVE john_org(id, name, manager_id, level) AS
( SELECT id, name, manager_id, 1 AS level
  FROM
    employee
  WHERE
    RTRIM( name) = RTRIM( 'John')
  UNION ALL
  SELECT e.id, e.name, e.manager_id, level + 1 AS next_level
  FROM
    employee e,
    john_org j
  WHERE e.manager_id = j.id and level < 4
)
SELECT DISTINCT id, name, manager_id FROM
  john_org
ORDER BY manager_id;
Copy

ID

NAME

MANAGER_ID

101

John

100

102

Jorge

101

103

Kwaku

101

110

Liu

101

106

Mateo

102

201

Sofía

102

110

Nikki

103

104

Paulo

103

105

Richard

103

120

Saanvi

104

200

Shirley

104

205

Zhang

104

Non recursive form

Input Code:
 WITH ManagerHierarchy AS (
    SELECT id AS employee_id, name AS employee_name, manager_id
    FROM employee
)
SELECT e.employee_name AS employee, m.employee_name AS manager
FROM ManagerHierarchy e
LEFT JOIN ManagerHierarchy m ON e.manager_id = m.employee_id;
Copy

EMPLOYEE

MANAGER

Carlos

null

John

Carlos

Jorge

John

Kwaku

John

Liu

John

Mateo

Jorge

Sofía

Jorge

Nikki

Kwaku

Paulo

Kwaku

Richard

Kwaku

Saanvi

Paulo

Shirley

Paulo

Zhang

Paulo

Output Code:
 WITH ManagerHierarchy AS (
    SELECT id AS employee_id, name AS employee_name, manager_id
    FROM
    employee
)
SELECT e.employee_name AS employee, m.employee_name AS manager
FROM
    ManagerHierarchy e
LEFT JOIN
    ManagerHierarchy m ON e.manager_id = m.employee_id;
Copy

EMPLOYEE

MANAGER

John

Carlos

Jorge

John

Kwaku

John

Liu

John

Mateo

Jorge

Sofía

Jorge

Nikki

Kwaku

Paulo

Kwaku

Richard

Kwaku

Saanvi

Paulo

Shirley

Paulo

Zhang

Paulo

Carlos

null

Related EWIs

There are no known issues.