From the course: Quick Start Guide to SQL

Basic select statements

- [Instructor] Let's learn how to write a basic select statement and how it is used in various scenarios. First of all, why do we need a select statement? What does it do? In a database, there are multiple tables containing huge sets of data. So the main functionality of a select statement is to fetch the data based on our criteria. These are the three scenarios in which we use a select statement. The first scenario is when we need to fetch data from any number of columns in a table, it could be from just one column or multiple columns or all columns in the table. The second scenario when we use a select statement is to fetch data from one or multiple tables. If it's from one table, it's a simple select statement. But if we are fetching data from multiple tables then we make use of joints which we will learn in the later sections of this course. The third scenario when we use a select statement is to filter out the of row that are fetched from the table based on our search criteria. Now, here is the syntax for the select statement. We give select followed by * if you want to fetch all the columns from the table, or we give specific column names, or we give some expressions if you want to perform some kind of mathematical operations or if we want it to be displayed in a certain format, followed by FROM keyword, and then the table names. We will learn about distinct and alias column names in the later sections. Select identifies the columns that are retrieved from the table, and FROM keyword identifies the tables from where this data is being fetched. To understand this better, we will look at a few examples in Live SQL. Here, I will give SELECT * FROM emp_tab to retrieve all the column and all the rows from emp_tab table. So now to run this statement, I can click on this run button and if I'm using a Mac system, I will give Command + Enter, if I'm using a Windows system, I'll give Control + Enter. So I run this and it fetches all the rows and columns from emp_tab table. If I want to fetch only two columns in specific, maybe emp number and name, I'll give SELECT empno,name FROM emp_tab; and I run this. It fetches only those two columns. The best part about SQL is that there are very few rules to follow and we are good to go. SQL statements are not case sensitive, we can write them either in uppercase, lowercase, or even camel case, and it doesn't throw any errors. However, the standard that is followed across all the organizations is to use uppercase for keywords and lowercase for the rest of the code. Keywords cannot be abbreviated or split across lines. We can write the entire select statement in just one line, but the SQL statement which has proper indentation helps with readability especially when we are writing very long select statements. This is about a base six statement.

Contents