SQL with Jupyter Notebook Tutorial

18 / 61

Selecting data from a table

Syntax:

select field1, field2,...fieldN 
[from table_name1, table_name2...]
[where clause]
[limit [offset M ][limit N]]

When selecting a fixed value, FROM clause is not mandatory.

%%sql
select 'Hello';
  • select * -> Return all fields
  • select col1, col2 -> Return specified fields
  • offset -> To skip initial records
  • limit -> How many rows to return

SELECT examples:

%%sql
select * from deptt_<<your lab username>>;
select d.* from deptt_<<your lab username>> d;
select * from deptt_<<your lab username>> d;
select depname from deptt_<<your lab username>> limit 1;
select depname from deptt_<<your lab username>> limit 1,2; -- skip 1 and return only 2 rows

Aliases

Tables and Columns can be given alias names for easy readability.

%%sql
select d.depname "Dept Name" from deptt_<<your lab username>> d;

Selecting unique values Values in columns of a table may repeat across the rows of the table. To get unique values, use select distinct.

%%sql
select distinct d.depname "Dept Name" from deptt_<<your lab username>> d;

No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...