SQL Basics to get you started
SQL stands for Structured Query Language and it is used to pull relevant data from the database. We assume that you already know a bit about SQL, so we will try to cover the base and then will quickly jump to some questions.
1. What all can we do with SQL?
We can perform the following task:-
a. Retrieval of
2. A table is basically a set of data which is organized in the form of rows and columns.
3. What are the different subsets of SQL?
We can further divide SQL into 3 parts or subsets:-
a. Data Definition Language – It helps you to CREATE, ALTER, and DELETE objects
b. Data Manipulation Language –It helps us to insert, update, delete, and retrieve data from the database
c. Data Control Language – DCL allows you to control access to the database. Grant and Revoke access permission
4. What is a Primary Key?
– A key which uniquely defines each row in a table and it cannot be NULL. Example – Roll number or Employee ID in a student or Employee table
5. What are
– Constraints are used to apply a limit on the data type of the table. Following are a few examples of constraints:-
a. Primary Key
b. Foreign Key
c. Unique
d. Default
f. Check
g. Not Null
6. What is a unique key?
– A key which uniquely defines each row in a table. It is mostly a group of keys which can or can not be NULL
7. What are the different types of Joins?
– There are various types of Joins, the most important out of these are:-
a. Inner Join – Intersection of two tables
b.
c. Left Join – All the values from the left table and their corresponding matched values from the right
8. We will not be talking about ACID properties and Normalization. These are very easily available on the internet. It would be very good if you watch a couple of videos on these topics on Youtube. The links of the videos are given below:-
Normalization
ACID Properties
9. What is a NULL value?
A NULL value is neither zero nor a blank space. It represents a value that
Remember zero is a number and blank space is a character
10. What is the order of SQL query execution?
Order | Clause | Function |
1 | FROM and JOIN | Select the table to extract from |
2 | WHERE | Filter the base data |
3 | GROUP BY | Agreegate the base data |
4 | HAVING | Filter the aggregate data |
5 | SELECT | Return the final data |
6 | DISTINCT | Apply distinct or unique condition |
7 | ORDER BY | Sort the final data |
8 | LIMIT | Apply limit on the number of rows |
11. What is the difference between DROP and TRUNCATE commands?
-DROP command removes a table and it cannot be rolled back from the database whereas TRUNCATE command removes all the rows from the table.
12. What is a view in SQL?
– Views in SQL are kind of virtual tables. A view also has rows and columns as they
specific rows based on certain condition
13. What is Union, minus and Interact commands?
UNION operator is used to
MINUS operator is used to return rows from the first query but not from the second query. Matching records of first and second query and other rows from the first query will be displayed as a result set.
INTERSECT operator is used to return rows returned by both the queries.
14.
a. BETWEEN
SELECT *
FROM Table Name
WHERE Date BETWEEN ‘date1’ AND ‘date2’
b. ORDER BY
SELECT *
FROM Table Name
WHERE Condition
ORDER BY {ASC/DESC}
c. HAVING
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithematic function condition);
d. TRUNCATE Table
TRUNCATE TABLE Table_Name
e. ALTER Table
ALTER TABLE Table_Name {ADD/DROP/MODIFY} Column_Name {Data Type}
f. INSERT INTO
INSERT INTO table_name( column1, column2….columnN)
VALUES ( val1, val2….valN);
g. UPDATE
UPDATE table_name
SET column1 = val1, column2 = val2….columnN=valN
[ WHERE CONDITION ];
15. Explain SQL clause
The SQL LIKE clause is used to compare a value to similar values using wildcard operators.
There are two wildcards used in conjunction with the LIKE operator.
The percent sign (%)
The underscore (_)
The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.
These symbols can be used in combinations.
16. What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’ CLAUSE?
HAVING clause can be used only with SELECT statement.
It is usually used in a GROUP BY clause and whenever GROUP BY is not used, HAVING behaves like a WHERE clause.
Having Clause is only used with the GROUP BY function in a query
whereas WHERE Clause is applied to each row before they are a part of the GROUP BY function in a query.
17. What is the difference between JOIN and UNION?
-UNION combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. By using JOINs, you can retrieve data from two or more tables based on logical relationships between the tables
JOIN:-
a.) Joins the columns
b.)Duplicates are
UNION:-
a.) Merge the
18. What is self join?
– The SQL SELF JOIN is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.
SELECT a.column_name, b.column_name…
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;
19.
SELECT – select records from a table
INSERT – insert new records
UPDATE – update/Modify existing records
DELETE – delete existing records
CREATE – create a new Table, database, schema
ALTER – alter existing table, column description
DROP – delete existing objects from
GRANT – allows users to read/write on certain database objects
REVOKE – keeps users from
20. What is the difference between inner and outer join? Explain with example.
–
If none of the
Outer Join- Outer Join, on the other hand, will return matching rows from both tables as well as any unmatched rows from one or both the tables (based on whether it is single outer or full outer join respectively)
These were the very basic questions and t
You can easily find the explanation of the above questions on the internet, else you can also look into our book on SQL
112 Questions to crack Data Science Interview using SQL
There are few more questions on TheDataMonk which you can find here
Keep Practicing !!
XtraMous