Updated March 17, 2023
Introduction
The following article provides an outline for SQL Examples. SQL is expanded as Structured Query Language which is used for data management and programming. It can also be used in stream processing and relational data stream management. The data can be incorporated into variables and entities. The SQL has readable and over-written VSAM or ISAM. It can be accessed using a single command and neglects the requirement to modify the record using or not using the index.
Examples of SQL
The important examples in SQL are explained in this article.
SQL Syntax is used to select all the relevant data from the given table under (“courses”).
SQL SELECT data ("courses")
SQL SELECT ROWS
SELECT * courses
SELECT NUMBER (COURSES number_name)
SELECT NUMBER (COURSES number_name) temporary can be applied for MS access
The select option is used to define the required column of information where the user to view the results. Other options are also available to display the data which is not under the table column. The example displays the column, that is selected under the “courses” table. The beginning column will be pointless number and the next will display the system date.
select courseID, coursename, coursefee, duration,
3+12 as fifteen, now() as currentday
from course;
SQL ORDER BY option is used to sort the result in organized way where the items can be managed using SELECT options and the corresponding list can be sorted using course name, fees, or course id. If the user wants to sort the data in ascending order use ASC option and descending use DESC option as per the below example.
select courseID, coursename, coursefee, duration,
from courses
where
(
courseid between 1
and 5 -- inclusive
or courseid = 8
or coursename like '%AWS%'
)
and coursefee NOT in (10000, 14000)
order by coursefee DESC;
SQL Group by option provides the user to merge rows and club the data. Here having a phrase is similar to where phrase and it acts like the grouped data. The data is fetched from the pool which can be practiced by using simple commands.
TRUNCATE option in SQL is used to remove all the elements from the table and it works rapidly than the delete statement. It is mostly used in transaction log sections.
TRUNCATE option works ideal for higher dimensional data management.
TRUNCATE TABLE Courses;
The CREATE DATABASE is used to build a new database works followed by the CREATE TABLE with other options to build a new database and set available to use.
ALTER DATABASE is used to edit the files or filegroups in the database.
DROP DATABASE in SQL is used to remove or delete the database.
CREATE TABLE in SQL enables the user to build a table in the database and when the user builds a table, he must mention the column and its datatype along with other dependencies or required settings.
In the below example, the table is created called courses with two columns and course id and course name is set as a primary key.
CREATE TABLE courses
(
Course id INT NOT NULL AUTO_INCREMENT,
Course name VARCHAR(255) NOT NULL,
PRIMARY KEY (coursename)
);
ALTER table in SQL is used to modify the table definition and it provides the specific datatype.
SQL Statements
The SQL statement is used to answer the question in a comparative method or it even answers to multiple question frames like, which student enrolled for multiple courses more than two but not less than five. So this type of query can be executed by the below program. The data can be sorted in descending order, to find the students enrolled in maximum courses, as they will be viewed in the top.
SQL SELECT option is used to fetch the information from the database and it can be chosen in one or multiple tables and it can be retrieved from a specific column as per the user’s requirement.
The below example chooses two columns from the courses table.
SELECT coursename, coursefee
FROM courses;
The below example fetches all columns from the courses table.
SELECT * FROM courses; WHERE course id ='1001'
select name, courseid, sum (Total_$), count(*)
from courses
where coursecapacity = 120
group by name, coursecapacity
having coursecapacity count (*) > 5
order by count (*) DESC;
The result set includes the option with data that has a course id of 1001. The user can still refine the data using = operator. It can be used to compare the similarity option using LIKE phrase. Here the % sign is used to compare the issues. The user can filter the data which course offers and same it applies to other part of data like course id, course fees, and duration.
SELECT * FROM Courses WHERE courseofffer LIKE '25%'
The same % sign can be applied at multiple times using LIKE phrase.
The other option commonly used in SQL statement is _ which helps to find the character in same pattern.
For example,
Any course fees should be with 25% offer.
It can be framed like:
SELECT * FROM Courses WHERE Courseoffer LIKE '_25%'
The other option used along with SELECT is TOP statement. It is used to constrain the count of rows according to the result of the code. If the user wants to fetch only two rows, he can use the below example.
SELECT TOP (2) * FROM Courses
The statement can be limited using % symbol also, that is called as a top percent.
SELECT TOP (50) PERCENT * FROM Courses
INSERT option in SQL is used to add new data into the table. In below example, the user can add new row offer in the courses table
INSERT INTO courses (coursename, courseid) OFFER
("AWS" "1001, "25%");
By using the default option, the beginning column with default values has been defined in the column and it can be configured again as per the requirement of the user.
UPDATE statement in SQL enables the user to update one or multiple records in database.
In the below example, the course offer rows is updated in the courses table
UPDATE courses
SET courseoffer = "50%"
WHERE courseoffer = "25"%;
DELETE option in SQL is used to remove the specified column from the table.
This option works along with WHERE phrase to narrow down the search option to find the deleted rows.
DELETE FROM Courses
WHERE Courseid = '1001';
SQL DATA DIFF and AGGREGATE Function
The user combined the tables as the data is required from both tables course and account. So SQL provides aggregate function called SUM to calculate the account and combines with course capacity. There are no extra rules when the result of two table is combined and executed by other function SUM and DATA DIFF without any issues.
SQL AVERAGE function can be calculated by using the data difference and aggregate function. The values can be replaced with sum and average. The query executes the average value and here no need to use any group by option as the rows are already placed in the group. So aggregate option eliminates the use of group by option.
Conclusion – SQL Examples
Hence in this article simple and standard examples of SQL and its statement are discussed in brief. The queries can be implied in higher dimensional database and it can be executed according to requirement of user.
Recommended Articles
We hope that this EDUCBA information on “SQL Examples” was beneficial to you. You can view EDUCBA’s recommended articles for more information.