SQL Examples

SQL Examples

How to run SQL commands:
Here are some examples...

1. Create Statement - Using create statement , we can create table or database.

  • Example: Using create statement for database creation
       create database database_name;



  • Example: Using create statement for table creation.
       create table table_name (column1 datatype, column2 datatype, column3 datatype, ...);


That's how you create table in the database. Now, we need to enter data in the database.



2. Insert Statement - Using insert statement , we can insert data in the table.

There are two ways to insert data in the table using insert statement.

  • Example 1: Enter data in the table using insert statement (1st way)
insert into table_name values (value1, value2, value3, ...);



  • Example 2: Enter data in the table using insert statement (2nd way)
insert into table_name (column1, column2, column3, ...) values (value1, value2, value3, ...);





3. Select Statement - Using select statement , we can retrieve data from database.

  • Example 1: Retrieve data from database using select statement. By using this query, we can retrieve whole data of the table, means all columns will be retrieved.
select * from table_name;


  • Example 2: By using this query, we can retrieve selected columns.
select column1, column2, column3, ... from table_name;




4. Drop Statement - Using drop statement , we can delete table from the database.

  • Example: Drop table table_name;

  • Verified: Table has been deleted.




5. Truncate Statement - Using truncate statement , we can delete all rows from the table.

  • Example: Truncate table table_name;
First, lets check available data in the database

Now, lets use the truncate command

  • Verified: Table is empty.





6. Delete Statement - Using delete statement , we can delete rows on the basis of condition. Like if we want to delete a particular rows.

  • Example: Delete from table_name where condition;
First, lets check available data in the database


Now, lets use the delete command

  • Verified: Available data in the table.





6. Alter Statement - Using alter statement , we can modify the structure of the table. We can also add or drop constraint from the table.


1. Adding column in the existing table
Example: Alter table table_name add column column_name datatype;

  • Checking existing table.

  • Adding new column

  • Verified : Updated table



2. Deleting column from the existing table.
Example: Alter table table_name drop column column_name;

  • Checking existing table.


  • Deleting column

  • Verified : Updated table




3. modifying column name in the existing table.
Example: Alter table table_name rename column old_column_name to new_column_name;

  • Checking existing table.


  • Renaming column name

  • Verified : Updated table (column name has been changed)




4. Modifying column data type in the existing table.
Example: Alter table table_name change column column_name type new_datatype;

  • Checking existing table.

  • Changing column data type


  • Verified : Updated table (column data type has been changed)





7. Update Statement - Using update statement , we can update the existing data in the table.


Example: update table_name set column1=value1, column2=value2, ... where condition;

  • Checking existing table.


  • Updating data


  • Verified : Updated table (employee data has been updated on the basis of condition)

Comments

Popular posts from this blog

SQL

SQL Commands