Types of command in sql part 2 | LEARN SQL

Hey Guriilians welcome to the continuing series of SQL in previous blogs we learned about the types of commands in SQL, select command, where clause, types of keys, identifiers keywords, and views If you haven’t read about that you can read it now too.



Let’s continue with our topic:-

Today we’ll be studying these topics

1. ALTER COMMAND

2. SQL COPY TABLE

3. DISTINCT FUNCTION

4. TYPES OF SELECT FUNCTIONS

ALTER COMMANDS:-

In SQL, the ALTER command is used to modify the structure of an existing database object, such as a table, view, or index.

ALTER TABLE: This command is used to modify the structure of a table. You can use this command to add, modify, or delete columns in a table, or to change the data type or size of a column. The syntax for the ALTER TABLE command is:

ALTER TABLE table_name

action1,

action2,

...

FOR EXAMPLE:-

Table name=guerillateck



Output:-


Note*- When you add a column to the table so every value becomes null of the newly added column.

ALTER VIEW: This command is used to modify the definition of a view. You can use this command to change the SELECT statement that defines the view or to change the name of the view. The syntax for the ALTER VIEW command is:

ALTER VIEW view_name
AS new_select_statement;

For example:-


OUTPUT:-


ALTER INDEX: This command is used to modify the structure of an index. You can use this command to add or remove columns from an index, or to change the order of the columns in the index. The syntax for the ALTER INDEX command is:

ALTER INDEX index_name action1, action2, ...

SQL COPY TABLE:-

With the help of the SELECT INTO command in SQL, it is possible to transfer data from one SQL table to another SQL table on the same SQL server.

A table's contents are transferred into a new table using the SELECT INTO statement in Structured Query Language. Utilizing the structure of the original table, SQL builds a new table.

Syntax of SELECT INTO statement in SQL

SELECT * INTO New_table_name FROM old_table_name;

FOR EXAMPLE:-


OUTPUT


DISTINCT FUNCTION

In SQL, the DISTINCT function is used to retrieve unique or distinct values from a specified column or columns in a table. It eliminates duplicate rows from the result set of a SELECT statement.

Here is the basic syntax for using the DISTINCT function:

SELECT DISTINCT column_name
FROM table_name;

In this syntax, column_name is the name of the column from which you want to retrieve distinct values, and table_name is the name of the table from which you want to retrieve the data.

For example, suppose you have a table called guerillateck that contains the following data:

For example:-


Output:-


We had already learned about the select command in previous blog lets revise then we start with further types for select.

SQL SELECT Statement

The command in Structured Query Language that is used the most is the SELECT statement. The records from one or more database tables and views can be accessed using this method. Additionally, it retrieves the chosen data that adheres to our desired criteria.

By using this command, we can also obtain the specific record from the specific table column. The table that contains the record that the SELECT statement returned is referred to as a result-set table.


Syntax of SELECT Statement in SQL

SELECT Column_Name_1, Column_Name_2, ....., Column_Name_N FROM Table_Name;

In this SELECT syntax, Column_Name_1, Column_Name_2, ….., and Column_Name_N is the name of those columns in the table whose data we want to read.

If you want to access all rows from all fields of the table, use the following SQL SELECT syntax with * asterisk sign:

SELECT COUNT FUNCTION

In SQL, the SELECT COUNT function is used to count the number of rows in a table that meets certain conditions. It can be used with or without the WHERE clause to count all rows in a table or only those rows that meet specific criteria.

Here is the basic syntax for using the SELECT COUNT function:

SELECT COUNT(*) FROM table_name WHERE condition;

In this syntax, * is a wildcard that specifies all columns in the table, table_name is the name of the table that you want to count the rows from, and the condition is an optional clause that specifies one or more conditions that the rows must meet to be included in the count.

For example:-


This will show the no. of std in class 11 in table guerillateck.

Output


SQL SELECT TOP

The small number of records or rows from the database table is displayed using the SQL SELECT TOP statement. How many rows are returned is specified by the TOP clause in the query.

In the output, the top N rows from the tables are displayed. When the database tables contain thousands of records, this clause is used.

Syntax of TOP Clause in SQL

SELECT TOP number | percent column_Name1, column_Name2, ....., column_NameN FROM table_name WHERE [Condition] ;

Let table guerillacars

carname

carcolor 

carcost

mercedes benz  

white                

800000

Hyundai Creta 

white

1085000

Hyundai Venue 

white

950000

Hyundai i20  

Red

900000

Kia Sonet     

white

900000

Kia Seltos

Black  

8000000




Output






so that's it for today guys, follow us to learn more about SQL topics.....

happy learning.....

No comments:

Post a Comment