EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL Examples
Secondary Sidebar
SQL Tutorial
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • SQL Quick References
    • SQL Like Wildcard
    • SQL Like with Multiple Values
    • SQL Examples
    • SQL Server Versions
    • SQL DROP DB
    • SQL Case Insensitive
    • SQL Expressions
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL WAITFOR
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • SQL GROUP BY WHERE
    • SQL ROW
    • SQL EXECUTE
    • SQL EXCLUDE
    • SQL Performance Tuning
    • SQL UUID
    • Begin SQL
    • SQL Update Join
    • SQL Between Dates
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • sqlplus set commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL SELECT DISTINCT Multiple Columns
    • SQL Null Values
    • SQL LIKE
    • SQL LIKE Query
    • SQL LIKE Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • SQL HAVING Clause
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DESC
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • SQL Foreign Key
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • SQL UNIQUE Constraint
    • SQL Primary Key
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL Server Functions
    • SQL String Functions
    • SQL Compare String
    • Timestamp to Date in SQL
    • SQL REGEX
    • SQL Window Functions
    • SQL Syntax
    • SQL CONCAT
    • SQL ALTER TABLE
    • SQL MOD()
    • SQL Timestamp
    • SQL Min and Max
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • SQL HOUR()
    • SQLite? functions
    • ANY in SQL
    • LIKE Query in SQL
    • SQL NOT NULL
    • SQL NOT IN
    • SQL MAX()
    • SQL MIN()
    • SQL SUM()
    • SQL COUNT
    • SQL identity
    • SQL DELETE Trigger
    • SQL Declare Variable
    • SQL Text Search
    • SQL COUNT DISTINCT
    • SQL TEXT
    • SQL Limit Order By
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Ranking Function
    • SQL Server Permission
  • T-SQL
    • T-SQL pivot
    • T-SQL Formatter
    • T-SQL TRY CATCH
    • T-SQL CTE
    • T-SQL CASE
    • T-SQL DATEPART
    • T-SQL Date Format
    • T-SQL ROUND
    • T-SQL Loop
    • T-SQL IIF
    • T-SQL Union
    • T-SQL CREATE TABLE
    • T-SQL INSERT
    • T-SQL Stuff
    • T-SQL ISNULL
    • T-SQL ADD Column
    • T-SQL DATEDIFF
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • LEFT OUTER JOIN in SQL
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • MDF File in SQL Server
    • SQL Aliases
    • SQL Hosting
    • SQL Auto Increment
    • SQL Injection
    • SQL Wildcards
    • SQL Check
    • SQL Indexes
    • Select Distinct
    • SQL BETWEEN
    • SQLPlus spool
    • SQL Create Table
    • SQL Schema
    • Comparison Operators in SQL
    • SQL_plus
    • SQL Formatter
    • SQL LEFT INNER JOIN
    • SQL Plus Command
    • SQLPlus not found
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • SQL REVOKE
    • SQL Select Distinct Count
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL BIGINT
    • SQL Crosstab
    • SQL Wildcard Character
    • SQL INSTR()
    • SQL now
    • SQL synonyms
    • SQLite?export to csv
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • SQL DROP Table
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • ROLLUP in SQL
    • Escape Character SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
    • SQL if then else
    • SQL ignore-case
    • SQL Matches
    • SQL Search String
    • SQL Column Alias
    • SQL extensions
    • SQL Substring Function
    • Charindex SQL
  • SqlAlchemy
    • What is SQLAlchemy
    • SqlAlchemy ORM
    • SQLAlchemy count
    • SQLAlchemy update object
    • SQLAlchemy pip
    • SQLAlchemy Connection
    • SQLAlchemy Transaction
    • SQLAlchemy Metadata
    • SQLAlchemy Raw SQL
    • SQLAlchemy Filter in List
    • SQLAlchemy Alias
    • SQLAlchemy unique
    • SQLAlchemy JSONB
    • SQLAlchemy Async
    • SQLAlchemy Types
    • SQLAlchemy Many to Many
    • SQLAlchemy Example
    • SQLAlchemy Model
    • SQLAlchemy Data Types
    • SQLAlchemy Filter
    • SQLAlchemy SQLite
    • SQLAlchemy DateTime
    • SQLAlchemy create_engine
    • SQLAlchemy Delete
    • SQLAlchemy Migrations
  • NoSQL
    • NoSQL Databases List
    • NoSQL Data Modeling
    • Types of NoSQL Databases
    • NoSQL Injection
    • NoSQL vs SQL Databases
    • NoSQL Use Cases
    • NoSQL Key Value
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions
    • SQL Current Month

SQL Examples

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.

SQL Examples

Examples of SQL

The important examples in SQL are explained in this article.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

  1. SQL REGEX
  2. SQL Like Wildcard
  3. SQL Server Functions
  4. SQL Injection
Popular Course in this category
JDBC Training (6 Courses, 7+ Projects)
  6 Online Courses |  7 Hands-on Projects |  37+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

PHP Training (5 Courses, 3 Project)4.9
Windows 10 Training (4 Courses, 4+ Projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
PL SQL Training (4 Courses, 2+ Projects)4.7
Oracle Training (17 Courses, 8+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more