...

/

Introduction to SQL Statements

Introduction to SQL Statements

Learn and practice SQL statements.

The basic building block of SQL is a statement. Chaining multiple statements with the ; delimiter we arrive at a complete SQL script. To that extent, consider the following SQL code:

Press + to interact
-- Remove any previous definition of `date_and_time`.
DROP TABLE IF EXISTS date_and_time;
-- Generate a temporary table for the showcase of temporal data types.
CREATE TEMPORARY TABLE date_and_time
(
date DATE DEFAULT (CURRENT_DATE),
time TIME DEFAULT (CURRENT_TIME),
datetime DATETIME DEFAULT NOW(),
timestamp TIMESTAMP DEFAULT NOW(),
year YEAR DEFAULT (CURRENT_DATE)
);
-- Record an empty row of temporal data types.
INSERT INTO date_and_time VALUE ();

This SQL script contains three statements:

  • Ensuring any existing table named date_and_time is dropped from the database to prevent errors from attempting to create a table that already exists. It's a way to start fresh with the upcoming table definition (line 2).

  • Defining the creation of a temporary table named date_and_time. This table is meant to showcase various temporal data types in SQL. Each column is designed to store a different kind of temporal data (lines 5–12)

  • Inserting a new row into the date_and_time table without specifying any values (VALUE ()). Due to the default values ...

Access this course and 1400+ top-rated courses and projects.