SQL Queries: The Top 10 Most Used
SQL Queries: The Top 10 Most Used
3% cover 90% of regular operations on database. Before we start talking about them, it is good to know that these
10 queries work on all types of SQL engines available in the market. For an introduction to SQL, try this course
Demystifying SQL
SQL is just an interface which helps you communicate with
your systems database using queries. We rotate our cars
steering wheel without bothering about what mechanical
reaction might have taken place in our wheels axle. We are
only concerned about turning our car. Similarly when we use
SQL, we only have to fire simple queries to retrieve data from
your database without thinking about internal database
operations.
POPULAR POSTS
How to Build an iPhone App from Scratch for
Non-Technical People: Your quick and dirty
guide
Excel Formulas: 10 Formulas That Helped Me
Keep My Job
Code Wars: Ruby vs Python vs PHP [Infographic]
Top 10 Programming Languages to Learn in
2014
How to Add Ringtones To Your iPhone (Updated
for iOS 7)
8 Best PowerPoint Presentations: How To Create
Engaging Presentations
Java Interview Questions: How to crack the TOP
15 questions
There are different types of SQL engines available free online. However, for beginners I recommend SQLite3
which you can download
here.
Once youve got it up, you should have a SQL prompt on your screen
Create a table
We start by creating an empty table by firing the following query
CREATETABLEstudent(idINTEGERPRIMARYKEY,nameTEXT,ageINTEGER);
This will create a table called student with 0 rows in it. This table will store records of students. It will have
student ID , student name & student age. Its structure will be like this:
ID (Primary key)NameAge
Here, the column ID is a primary key. It means that this column can have only unique values. Also it cannot be left
blank. Name column can have characters and age column will have age in numbers.
Try this course
INSERTINTOstudent(id,name,age)VALUES(1,alan,28);
Page Saved
Add Tags
Specify the column names in order after the table name in INSERT INTO statement and then values you want
after VALUES keyword.
After firing this query, our table will look like:
IDNameAge
1 alan 28
We can fire more such queries to fill records in our table.
insertintostudent(id,name,age)values(2,'amy',26);
insertintostudent(id,name,age)values(3,'bob',27);
insertintostudent(id,name,age)values(4,'chris',28);
insertintostudent(id,name,age)values(5,'dan',26);
27
4 Chris 28
5 Dan 26
2. Viewing all records from a table.
It is simplest of all & most frequently used query. Just type
SELECT*FROMstudent;
The result of this query will be a display of all rows present in the table.
IDNameAge
1 alan 28
2 Amy 26
3 Bob
27
4 Chris 28
5 Dan 26
We can also use ORDER BY clause in our select statement to arrange the displayed result in a particular order. For
example,
SELECT*FROMstudentORDERBYage;
27
1 alan 28
4 Chris 28
The output is arranged in increasing order of age. We can use DESC keyword after the column name in query if
we want to arrange the display in decreasing order.
3. Viewing only selected records from a table
If there are a huge number of rows in a table and we do not want all the records to fill our display screen, then
SQL gives us an option to view only selected rows.
SELECTCOUNT(1)FROMstudent;
Page Saved
IDNameAge
1 Alan 28
If we fire:
SELECTCOUNT*FROMstudent;
It will return the number of rows our table has. We can also use MAX & MIN function in our query. For example, if
we want to retrieve details of a student with maximum age, we can fire:
SELECTid,name,MAX(age)FROMstudent;
Add Tags
We will get:
IDNameAge
1 Alan 28
4 Chris 28
We can also check sum of a numeric column. For example,
SELECTsum(age)FROMstudent;
DELETEFROMstudentWHEREname=alan;
This query will delete the entire row, or more than one rows, from table student where name column have value
alan.
In our case, the result of this query will be following table
IDNameAge
2 Amy 26
3 Bob
27
4 Chris 28
5 Dan 26
Break time!
A SQL query goes into a bar, walks up to two tables and asks,
Can I join you?
I got a good chuckle out of that one. Moving on!
5. Changing data in existing records in a table
Suppose we want to change the age of a student named Amy
in our table. We would fire this query:
UPDATE student SET age = 22 WHERE name = amy;
You might have observed that we are specifying name in
where values are characters. This is a must.
Now if we fire:
SELECT*FROMstudent;
27
4 Chris 28
5 Dan 26
Be careful while you are firing UPDATE or DELETE queries with the help of the WHERE clause. Suppose in our
table student there is more than one student with name Amy. In this case, age of all students with the name
Amy will be updated to 22. That is why it is always preferred to use PRIMARY KEY in WHERE clause while
updating or deleting.
We also need to take care of datatypes in a column while we are changing data in it. A numeric column can have
only numbers in it while a text column can have text. This means that if we try to put age = Amy in age column
using UPDATE statement, SQL will throw an exception. You can learn more about types of errors and exceptions
that occur in SQL. More on this with another
Page Saved
Add Tags
of Dan but I am not sure how he spells his name. Whether it is Dan OR Den. In this case we can use LIKE
operator provided by SQL.
We will fire the following query.
SELECT*FROMstudentWHEREnameLIKEd%n;
26
27
4 Chris 28
5 Dan 26
6 Dan 24
Now if we fire our query as
SELECT*FROMstudentWHEREname=dan;
SELECT*FROMstudentWHEREname=danANDage=24;
SELECT*FROMstudentWHEREname=danORage>25
Output will be
IDNameAge
3 Bob
27
4 Chris 28
5 Dan 26
6 Dan 24
You can use different conditions like AND , OR , < , > in a combination or individually in WHERE clause to fetch
the desired rows. Try doing it yourself.
Learn how to use SQLs
SELECTnameFROMstudentWHEREage>25;
Page Saved
Add Tags
We can observe that only names of students are printed. Here we got names of only those students whose age is
greater than 25 because of a specified condition in WHERE clause.
We can also use more than one column names in SELECT statement separating them with a ,
For example:
SELECTname,ageFROMstudent;
27
Chris 28
Dan 26
Dan 24
You can also change the sequence of columns to be displayed on your screen. For example:
SELECTage,nameFROMstudent;
Add Tags
EXPLAINQUERYPLANSELECT*FROMstudent;
You can use EXPLAIN before a SQL statement to obtain breakup of the timings of the various parts of query. Its
useful for digging the reason behind a slow query.
In PostgreSQL, you can use EXPLAIN and EXPLAIN ANALYZE before your query. TheBeginners
Guide to
Training.
And that tops off the Top 10! For more comprehensive training, try these courses below:
Learn how to query and manage data in the Oracle Database Using SQL.
Practical, concept-building examples and quizzes.
Oracle SQL fundamentals
Microsoft SQL Server 2012 Certification Training Exam 70-463
Page Saved
to SQL
Page Saved
Add Tags