Introduction to SQL Commands
SQL or popularly known as Structured Query Language, is the fundamental query language for databases and is a domain-specific language for Relational Database Management Systems. It is useful when the data type is structured and there exists a dependency among the various attributes of data. It is widely used and is one of the most popular query languages for its unique capability of fetching multiple records by just making use of a single query statement. It consists of both DDL (Data Definition Language) and DML(Data Manipulation Language) commands which makes the use of queries much more efficient. SQL is a declarative programming language that is set-based and is therefore not an imperative programming language such as C or BASIC, etc. In order to fetch the data results from the database, you need to know SQL commands which we are going to study in this post.
Basic SQL Commands
The basic commands are as explained below.
1. SELECT: This is possibly the most basic SQL command. The select command is used to fetch or retrieve results from a particular table of a database. This is generally followed by specific column names or all column names (by making use of * ).
2. CREATE: This is one of the basic DDL commands which is used to create a table inside a database. The prerequisites of creating a table include knowing column name and their datatypes.
3. ALTER: This DML command is used to make alterations to the table. It includes modifying the table as per the need.
4. WHERE: The WHERE clause is one of the most important SQL commands as it is used to retrieve or fetch the specific data that is required for a particular case. This is helpful as it excludes all the irrelevant data.
5. SQL ALIAS: The ALIAS is among the most common SQL command which is used to give a meaningful name to your attribute or a column. They are defined for columns and tables, which helps in increasing the readability of the query.
6. AND: This is an SQL operator which is used to combine two conditions. For the row to appear in the data result set, both the conditions should be true.
7. AVG(): This is an aggregate function that is used to return the average value of a particular numeric column
8. ORDER BY: This is used along with the select statement to sort the results either in ascending order or descending order. If no argument is given after the ORDER BY clause, it by default takes as the ascending order query.
9. GROUP BY: The GROUP BY clause in the SQL commands is used to retrieve the data on the basis of some particular grouping related to one or more columns.
10. INSERT: The INSERT command is used to add a new row of data inside the table. The mandate for the INSERT command is that the table should be already created.
The intermediate commands are as explained below.
1. UPDATE: This command is used to update or modify the existing rows in a table.
2. DELETE: As the name suggests, this command is used to delete the rows from a table.
3. HAVING: The HAVING clause is used to filter the data based on one or more group functions. This is quite very similar to using a WHERE clause but this includes the use by a group function.
4. LIKE: This operator is used to compare between the two conditions and lists down all the rows of a table whose column name matches the pattern specified with the LIKE clause. Wildcard operators such as % are used when you are not sure about the particular pattern.
5. IN: The IN operator when the comparison of a column is made with multiple values. It is more or less similar to the OR condition.
6. IS NULL: The IS NULL operator is another SQL-based operator which is used to display whether the particular column name has a NULL value. It is used to fetch all the rows for the specified columns for which the data has not been found or has been found as null.
7. BETWEEN: As the name suggests, this operator filters the results for a particular set of ranges. The value, in this case, can either be numbers, dates or text.
8. CASE: This case is the same as used in other programming languages such as Java, etc., where a decision control statement is taken by the case statement and the argument inside it is used to fetch the case results matching the particular condition.
9. COUNT(): This is one of the most useful SQL commands which is used to display the count of the total number of rows for all those non-null data. It makes use of the column name as the argument.
10. INNER JOIN: This is one of the most important SQL commands which comes into play when there has to build a relationship among the attributes of two tables based on a fixed common attribute. In other words, the join condition has to be true in order for INNER JOIN to function.
Advanced SQL Commands
The advanced commands are as explained below.
1. LIMIT: The LIMIT clause will help you specify the maximum number of rows that are allowed in the result set.
2. OUTER JOIN: This is the join function that comes into play when all the fields of both tables are required based on some common input column condition. If the join condition is not met, then a NULL value will be filled on the right side of the column.
Tips and Tricks to use SQL Commands
Whenever you are using SQL commands, spend more time analyzing the table than querying and executing it. Make use of simpler and less complex queries with minimum use of dynamic SQL. Also, I prefer to use a table alias for better readability.
This post was about the important SQL commands which form the base of the database querying language. I hope you liked the article. Stay connected.
This has been a guide to SQL Commands. Here we have discussed basic, intermediate as well as advanced SQL Commands along with tips and tricks to use. You may also look at the following article to learn more-