The Wayback Machine - https://web.archive.org/web/20240822161842/https://www.geeksforgeeks.org/basic-sql-commands/
Open In App

Basic SQL Commands

Last Updated : 26 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

QUESTION: Consider the following table ITEM given below, write the commands in SQL for 1 – 10 and output for 11 – 20

                                                                                       TABLE: ITEM

S_No Item_Name Company Cost Purchase_Qty DOP
1 Monitor ABC 6300 20 2010-05-30
2 CPU CDF 8000 30 2010-07-23
3 Mouse FGH 250 25 2010-08-04
4 UPS HIJ 2100 10 2010-06-19
5 Printer JKL 8400 4 2010-11-27
6 Scanner JKL 4500 6 2010-08-14

Write the SQL commands for 1. to 10.

1. To display the name of the item and cost of those items which were purchased after ‘2010-08-05’.

Select Item_Name,Cost from ITEM where DOP>"2010-08-05";

2. To display information of those items whose purchase quantity is more than 10 arranged by Date of Purchase.

Select * from ITEM where Purchase_Qty>10 ORDER BY DOP;

3. To display the average cost price of the items of ‘JKL’ Company.

Select avg(cost) from ITEM where Company="JKL";

4. To display all the information of those items whose name starts with letter ‘M’.

Select * from ITEM where Item_Name like "M%";

5. To display maximum cost price of the items which contain letter ‘J’ in the company name.

Select max(Cost) from ITEM where Company like '%J%;

6. To display all details of items whose purchase quantity is more than 5 arranged in decreasing order of Date of Purchase.

Select * from ITEM where Purchase_Qty>5 order by DOP desc;

7. To display S_No, Item_Name and cost of all those items where Item_Name has the letter ‘o’ in second position.

Select S_No,Item_Name,Cost from ITEM where instr(Item_Name,'o')=2;

8. To display Company, minimum of Purchase_Qty grouped by the month of purchase.

Select Company,min(Purchase_Qty) from ITEM group by month(DOP);

9. To show the structure of the table ITEM.

Desc ITEM;

10. To set Purchase_Qty as 31 of ‘JKL’ company. 

Update ITEM set Purchase_Qty=31 where Company='JKL'; 

Write the Output for the following SQL queries 11 – 20.

11. Select count(*) from item where cost>6000 and Purchase_Qty<25;
       OUTPUT:

count(*)
2

12. Select max(Purchase_Qty) from item where cost<5000;
       OUTPUT:

max(Purchase_Qty)
25

13. Select avg(Cost) from item where Purchase_Qty>25;
      OUTPUT:

avg(Cost)
8000

14. Select cost+200 as ‘Selling_Price’ from item where Item_Name=’Scanner’;
       OUTPUT:

Selling_Price
4700

15. Select S_No,Item_Name,Purchase_Qty from ITEM where Purchase_Qty>=10;
      OUTPUT:

S_No Item_Name Purchase_Qty
1 Monitor 20
2 CPU 30
3 Mouse 25
4 UPS 10

16. Select Ucase(Item_Name), Lcase(Company) from ITEM;
     OUTPUT:

Ucase(Item_Name) Lcase(Company)
MONITOR abc
CPU cdf
MOUSE fgh
UPS hij
PRINTER jkl
SCANNER jkl

17. Select S_No, DOP from ITEM where month(DOP)=8;
      OUTPUT:

S_No DOP
3 2010-08-04
6 2010-08-14

18. Select avg(Cost), Max(Purchase_Qty) from ITEM;
       OUTPUT:

avg(Cost) Max(Purchase_Qty)
4925 30

19. Select Item_Name from ITEM where Item_Name like “%e%”;
       OUTPUT:

Item_Name
Mouse
Printer
Scanner

20. Select * from ITEM where length(Item_Name)=3;
      OUTPUT:

S_No Item_Name Company Cost Purchase_Qty DOP
2 CPU CDF 8000 30 2010-07-23
4 UPS HIJ 2100 10 2010-06-19


Similar Reads

SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are very used to interact with the database. These commands allow users to perform various actions on a database. This article will teach us about SQL commands or SQL sublanguage commands like DDL, DQL, DML, DCL, and TCL. All important SQL commands with their syntax and examples are covered in this article. But before heading to the SQ
6 min read
Auto-commit commands and Compensating transactions in SQL
As the name suggests, the auto-commit statement is used to avoid the explicit mention of the commit command after each SQL statement. The commit statement itself gets executed by the server after each SQL statement. Each SQL statement is treated as a different transaction. An SQL statement executed in autocommit mode cannot be rolled back. Auto-com
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL vs NO SQL vs NEW SQL
SQL stands for Structured Query Language. Which is based on relational algebra and schema is fixed in this which means data is stored in the form of columns and tables. SQL follows ACID properties which means Atomicity, Consistency, Isolation, and Durability are maintained. There are three types of languages present in SQL : Data Definition Languag
2 min read
Basic Query in PL/SQL procedure
In this article, we will discuss the overview of PL/SQL and then our main focus on performing basic query operations in PL/SQL. And finally will conclude with Parameter modes in PL/ SQL subprograms. Let's discuss it one by one. Introduction :PL/SQL Stands for procedural language extension to SQL. In a procedure, the role of the subprogram is to per
3 min read
SQL Cheat Sheet ( Basic to Advanced)
In this article, we will explore the ultimate SQL cheat sheet with the PDF download, covering a basic to advance of SQL commands, Joins in SQL, CRUD Operations, SQL Trigger, SQL Transactions, and advanced topics to help master SQL effectively. SQL (Structured Query Language) is a Query language used for managing and manipulating relational database
15 min read
30 Days of SQL - From Basic to Advanced Level
This basic to advanced SQL tutorial covers the entire SQL syllabus in a structured way and provides the best learning material and strategies to master complete SQL in 30 Days. We have laid out the complete SQL roadmap, and following this roadmap, you will learn all the concepts of SQL. All Important concepts in the SQL syllabus like SQL queries, s
8 min read
DDL Commands &amp; Syntax
In this article, we will discuss the overview of DDL commands and will understand DDL commands like create, alter, truncate, drop. We will cover each command syntax with the help of an example for better understanding. Let's discuss it one by one. Overview :Data Definition Language(DDL) is a subset of SQL and a part of DBMS(Database Management Syst
3 min read
SQL | Difference between functions and stored procedures in PL/SQL
Prerequisite: Procedures in PL/SQLFunctions in PL/SQL.Difference between functions and stored procedures in PL/SQL Differences between Stored procedures(SP) and Functions(User-defined functions (UDF)): 1. SP may or may not return a value but UDF must return a value. The return statement of the function returns control to the calling program and ret
3 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
Basic operations and Working of LOB
This topic describes the usage and semantics of LOBs that you need to be familiar with to use LOBs in your application. Various techniques for working with LOBs are covered. Here, persistent LOBs assume that you are dealing with LOBs in tables that already exist whereas the task of creating tables with LOB columns is typically performed by your dat
5 min read
Basic approaches for Data generalization (DWDM)
Data Generalization is the process of summarizing data by replacing relatively low level values with higher level concepts. It is a form of descriptive data mining. There are two basic approaches of data generalization : 1. Data cube approach : It is also known as OLAP approach. It is an efficient approach as it is helpful to make the past selling
2 min read
Basic Object Oriented Data Model
Need of Object Oriented Data Model : To represent the complex real world problems there was a need for a data model that is closely related to real world. Object Oriented Data Model represents the real world problems easily. Object Oriented Data Model : In Object Oriented Data Model, data and their relationships are contained in a single structure
2 min read
Basic Operators in Relational Algebra
Basics of Relational model: Relational Model Relational Algebra is a procedural query language that takes relations as an input and returns relations as an output. There are some basic operators which can be applied in relation to producing the required results which we will discuss one by one. We will use STUDENT_SPORTS, EMPLOYEE, and STUDENT rela
3 min read
NULL values in SQL
In SQL there may be some records in a table that do not have values or data for every field and those fields are termed as a NULL value. NULL values could be possible because at the time of data entry information is not available. So SQL supports a special value known as NULL which is used to represent the values of attributes that may be unknown o
4 min read
DBMS | SQL | Question 1
The statement that is executed automatically by the system as a side effect of the modification of the database is (A) backup (B) assertion (C) recovery (D) trigger Answer: (D) Explanation: Triggers are the SQL codes that are automatically executed in response to certain events on a particular table. These are used to maintain the integrity of the
1 min read
DBMS | SQL | Question 2
Which of the following command is used to delete a table in SQL? (A) delete (B) truncate (C) remove (D) drop Answer: (D) Explanation: drop is used to delete a table completelyQuiz of this Question
1 min read
Article Tags :
three90RightbarBannerImg