SQL Basics
Querying Data
Filtering Data
Joining Tables
SQL Functions
Modifying Data
Defining Data
SQL CREATE Table
In this tutorial, you will learn how to create a new database table using the SQL CREATE statements.
Syntax
The basic syntax of the SELECT statement to select all columns, can be given with:
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/New-Project14.png)
CREATE TABLE table_name (
column1_name data_type constraints,
column2_name data_type constraints,
....
);
The asterisk (*) means everything. Once you use it after SELECT, you tell the database that you want all columns from the table.
Examples
To understand the SELECT statement in a better way, let’s look at the following customers and orders tables in our tutorial database:
![](https://www.datawithbaraa.com/wp-content/uploads/2021/10/createdb4-1024x178.png)
Now, let’s check out some examples that demonstrate how it actually works.
We have the following task be to solve using SQL statements
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/Tip3-1024x499.png)
The following Query will returns all the rows from customers table.
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/New-Project13.png)
CREATE TABLE db_sql_tutorial.persons (
id INT PRIMARY KEY AUTO_INCREMENT,
person_name VARCHAR(50) NOT NULL,
birth_Date DATE,
phone VARCHAR(15) NOT NULL UNIQUE
);
After executing the above query, you’ll get the result set something like this:
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/output-1024x283.png)
As you can see the output contains everything the whole customers tables including all rows and columns.
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/Tip-Copy.png)
SELECT * helps you to examine the content of table that you are not familiar with. But be careful using it specially with big tables because database will retrieve everything which costs a lot of data movement across the network and might slow down the application
Select specific Columns from Table
Let’s say we are only interested in getting only specific columns from a table, then we could use the following syntax of the SELECT statement:
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/New-Project14.png)
SELECT
column1,
column2,
columnN
FROM table_name
Insead of using asterisk (*) after SELECT, we will be now listing the name of columns of a table whose you want to retrive.
We have the following task to be solve using SQL statements
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/Tip3-1024x499.png)
In the task we dont reuqire all the data, we need only specific columns. The following SQL statement selcts only first_name and country from table customers.
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/New-Project13.png)
SELECT
first_name,
country
FROM customers
After executing the above query, you’ll get the result set something like this:
![](https://www.datawithbaraa.com/wp-content/uploads/2021/11/select-some.png)
As you can see the result contains only the columns that we specified after SELECT.