Prerequisites
For a description of how to configure various clients for issuing SQL commands, see Connecting.Data File
The tutorial script makes reference to a data file in the current directory. This path can be updated to point to a different path local to where the script is being run:Upload Data File
Insert Data from File
Creating a Type and Table
Before creating any database objects for this tutorial, a schema needs to be created to contain them:Create Tutorial Schema
REPLICATED specification.
The OR REPLACE specification can be added to ensure the table is replaced
if it already exists in the database. Adding a schema name before the table name
in the CREATE TABLE command, e.g., CREATE TABLE my_schema.mytable will
create the table under the given schema; however, the schema must first exist.
For this tutorial, three tables will be created:
- A base table containing a sample of NYC taxi cab ride data
- A lookup table containing sample cab vendor information
- A 1-to-1 relation table containing payment information for a subset of the cab-ride transactions
Create Vendor Table
Create Payment Table
Create Taxi Trip Table
Inserting Data
The INSERT INTO … VALUES statement is used to insert records into a table. At a minimum, each record must have values specified for all of theNOT NULL columns in the table. The
values specified in the VALUES clause must match the number and order of
the columns specified, or they must align with the ordering of the columns in
the table if no columns were specified:
Insert Data into Specified Columns
VALUES clause specifies values for all columns in
their natural table ordering (the order in which they appeared in the original
CREATE TABLE statement) no column names need to be specified, like so:
Insert Data into All Columns
Insert Data from KiFS File
Retrieving Data
A SELECT statement will retrieve the records from the given table. You can reduce the amount of records returned by using aTOP n clause. This will select the first n records returned by the query.
Use * to select all columns in the table.
Retrieve 10 Records
Retrieve All Records
Updating Records
The UPDATE statement is used to update matching key values for all records in a table. AWHERE clause can be used to specify the
records to update.
Update Records
Deleting Records
The DELETE statement is used to delete records from a table. AWHERE clause can be used to specify the records to delete.
Delete Records
Alter Table
Some properties can be altered or added after table creation, including indexes and dictionary encoding. Use the ALTER TABLE command to specify the table and property you want to alter.Indexes
ADD INDEX will add a column index to a table column.Add Index on Column
Dictionary Encoding
Use ALTER COLUMN to add column properties like dictionary encoding.Dictionary Encode Column
When adding a new property, all existing column properties must
be listed along with any new properties; those not listed will be removed.
Filters & Aggregates
A simple filter can be performed using the SELECT statement’sWHERE clause.
Filter by Value
This query makes use of the column index created in the preceding
Indexes section.
BETWEEN can be used to filter values within a range, inclusively.
Filter by Range
CASE ... WHEN statement is a logical function that allows you to
categorize records based on given filters.
Filter by Multiple Conditions
GROUP BY clause that groups the returned records by values in a given
column. The HAVING clause filters records after they’ve been aggregated.
Filter Aggregation by Value
Subqueries
Subqueries allow for nested queries within the clauses of a SQL statement. The below example is a simple SELECT statement that includes subqueries in each of its three clauses.Subqueries in SELECT, FROM, & WHERE Clauses
Common Table Expression / With
The WITH statement can be used to give a subquery an alias for use in a larger query. You can reuse the aliased query as if it were another result set, but only in the query immediately following theWITH
statement. The “parameters” to the WITH statement are the aliases that
will be given to the result columns returned by the SELECT contained within
the WITH; the number of “parameters” and columns in the SELECT clause
should match.
Filter Using CTE
Joins
Joins allow you to link multiple tables together, along their relations, retrieving associated information from any or all of them. Tables can only be joined if they’re sharded similarly or replicated. An inner join returns only records that have matching values in both tables.Inner Join
Left Join
Full outer joins may require both tables to be replicated or joined on
their shard keys. Set merges that perform deduplication of records, like
UNION DISTINCT, INTERSECT, and EXCEPT may also need to use
replicated tables to ensure the correct results, so a replicated version of
the taxi (taxi_trip_data_replicated) table is created at this point in
the tutorial.Create Replicated Table for Full Outer Join
Full Outer Join
Create Table As
You can also create a table directly from a query using CREATE TABLE … AS. These can also be created as temporary tables by applying theTEMP specification. Much like regular table
creation, you can also specify OR REPLACE to replace the table if it
already exists.
Create Temp Table from Query
Create Table from Query
Union, Intersect, and Except
A UNION can be used to combine homogeneous data sets into one larger data set.UNION & UNION DISTINCT will both combine data sets but
only retain the records that are unique across the chosen columns, removing all
duplicates. UNION ALL will combine data sets, retaining all records from
the source data sets.
Set Union Retaining Duplicates
INTERSECT ALL will retain duplicate intersecting records.
Set Intersection
EXCEPT will have duplicates removed first, and then the set subtraction
will be processed. EXCEPT ALL will retain duplicates from the first set.
Set Exception (Subtraction)
Truncate
Use TRUNCATE TABLE to remove all records from a table without deleting the table.Truncate Table
Complete Sample
Included below is a complete example containing all the above queries, the data files, and output. The script can be run via any SQL client, including KiSQL, as follows:Run Tutorial
As this script creates a schema and several database objects within
it, system admin permission
is required to run it.