SQL Tutorial

SQL Introduction SQL Aggregate Functions SQL Aliases SQL And SQL Any All SQL Avg SQL Between SQL Case SQL Comments SQL Count SQL Delete SQL Distinct SQL Exists SQL Groupby SQL Having SQL In SQL Insert_into SQL Is Not Null SQL Join SQL Full Outer Join SQL Inner Join SQL Left Join SQL Right Join SQL Self Join SQL Like SQL Min Max SQL NOT Operator SQL Null SQL Operators SQL OR operator SQL OrderBy SQL Select SQL Select Into SQL Top Limit Fetch SQL Store Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL BackupDB SQL Check SQL Constrains SQL Create View SQL CreateDB SQL CreateTable SQL Data types SQL Dates SQL DefaultConstrain SQL DropDB SQL DropTable SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL Dates Tutorial

Example Table

We will create a table named events to demonstrate various SQL date operations:


CREATE TABLE events (
    event_id INT PRIMARY KEY,
    event_name VARCHAR(100),
    event_date DATE,
    event_time TIME,
    event_timestamp TIMESTAMP
);

Creating the Table

To create the table, use the following SQL command:


CREATE TABLE events (
    event_id INT PRIMARY KEY,
    event_name VARCHAR(100),
    event_date DATE,
    event_time TIME,
    event_timestamp TIMESTAMP
);

Result:

Command CREATE TABLE events (event_id INT PRIMARY KEY, event_name VARCHAR(100), event_date DATE, event_time TIME, event_timestamp TIMESTAMP);
Result
  • A table named events is created with various date and time types.

Inserting Data into the Table

Let's insert some data into the events table:


INSERT INTO events (event_id, event_name, event_date, event_time, event_timestamp) VALUES
(1, 'Conference', '2023-10-01', '09:00:00', '2023-10-01 09:00:00'),
(2, 'Meeting', '2023-10-02', '14:00:00', '2023-10-02 14:00:00'),
(3, 'Workshop', '2023-10-03', '11:00:00', '2023-10-03 11:00:00');

Result:

Command INSERT INTO events (event_id, event_name, event_date, event_time, event_timestamp) VALUES (1, 'Conference', '2023-10-01', '09:00:00', '2023-10-01 09:00:00'), (2, 'Meeting', '2023-10-02', '14:00:00', '2023-10-02 14:00:00'), (3, 'Workshop', '2023-10-03', '11:00:00', '2023-10-03 11:00:00');
Result
  • Three records are inserted into the events table.

Querying the Table

We can query the events table to see the data:


SELECT * FROM events;

Result:

Command SELECT * FROM events;
Result
event_id event_name event_date event_time event_timestamp
1 Conference 2023-10-01 09:00:00 2023-10-01 09:00:00
2 Meeting 2023-10-02 14:00:00 2023-10-02 14:00:00
3 Workshop 2023-10-03 11:00:00 2023-10-03 11:00:00