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 |
|
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 |
|
Querying the Table
We can query the events
table to see the data:
SELECT * FROM events;
Result:
Command | SELECT * FROM events; | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Result |
|