Академический Документы
Профессиональный Документы
Культура Документы
School of Business
Hello everyone!! Welcome to MIS 310 SQL Tutorial!! In this tutorial we will learn the basics of SQL. We
will start from the installation of local server in your pc.
INSTALLING LOCAL SERVER
We will use Xampp in this tutorial,there are also other servers available like Wamp and Mamp.
Wamp is for windows, Mamp is for Mac OS. Xampp is available for both Windows and Mac OS.
XAMPP is a popular all-in-one kit that installs Apache, MySQL, and PHP in one procedure. XAMPP also
installs phpMyAdmin, a Web application you can use to administer your MySQL databases.
So, lets first install Xampp in your PC. If you dont have the installation file or if you havent collected
it yet, no need to worry, you can download it form http://www.apachefriends.org/en/xamppwindows.html .
Lets start the process. First open the folder where you saved your Xampp installation file.
1. Now, double click the installation file as shown in the Figure 1 and follow the process
2|P a ge
4. In this step you have to select a folder from your directory. Its c:\ xampp by default. Just click
the Install button if you dont want to install it anywhere else.
3|P a ge
4|P a ge
Ok.. So, finally your computer is ready for Mysql. Now open you web browser and in the address bar
type localhost to open your Xampp server.
Figure 9
A page like Figure 10 will appear in the screen where you will be asked to select a language. Select
English from the list.
5|P a ge
Click the phpMyAdmin link to open phpMyAdmin window. Your phpMyAdmin window will be exactly
like the figure 12.
Now we are ready to start our next section where wewill cover basic and some advance features of sql.
6|P a ge
After clicking SQL a new page like Figure 14 will appear. You will see a blank space where we are
7|P a ge
After typing the statement click GO button, right down corner of the window.
8|P a ge
So, we have created our first database!! Now if you look at the database list which is at the left side of
the window youll see the student database as shown in the Figure 17.
Click the student table to open the table. A new page will appear where you will see list of columns you
specified for your table with proper description.
10 | P a g e
As you can see id is the primary key of this table which we nominated while we were creating the
table. Our statement was:
`id` INT NOT NULL PRIMARY KEY,
It is also possible to write the statement like below:
CREATE TABLE `student` (
`id` INT NOT NULL,
`first_name` VARCHAR(100),
`last_name` VARCHAR(100),
`dept` varchar (3),
`address` VARCHAR(255),
primary key(`id`)
);
We can also add the auto increment feature to our id field. If we want to add auto increment, then we
just have to add auto_increment in the statement where we specified our id field, like below:
`id` NOT NULL auto_increment,
11 | P a g e
As you can see, in this SQL statement we first typed the statement we want to execute which is INSERT
INTO, then typed our table name student and after that we typed all the attributes/fields/column
names of our table inside bracket and finally typed the values we want to insert into our table using the
VALUESsyntax. After the VALUES syntax we specified all the data, separated by comma and order by
the column names that we specified before. You must remember, when youre writing a VARCHAR
element or text you always have to write it inside single quotation marks. But its not important when
you are inserting INT data. In this table our id field is auto increment enabled. Thats why we dont
have to write the id but you have to leave empty quotation marks.
This is a form of writing INSERT INTO statement. This form is very useful when you want to insert data
into specific columns. All you have to do is just to specify the column name/names where you want to
insert data. But if you want to insert data into all the columns, then you can just write,
INSERT INTO `student` VALUES ('', 'Faisal', 'Jahan', 'BBA', '41 Rampura, Dhaka');
Inserting Multiple rows:
Go to SQL page and write:
INSERT INTO `student`
12 | P a g e
Deleting Database:
To delete a database go to SQL and write
DROP DATABASE `database name`;
But by default the DROP DATABASE function is disabled in Xampp. So if you want to drop your database
then you have to follow the process below:
1. First go to http://localhost/phpmyadmin/ or click 127.0.0.1 from top.
13 | P a g e
2. Click Database.
3. Select your database name from the list and click drop button.
14 | P a g e
Deleting Tables:
To delete a table from your database first go to the SQL page and write
DROP TABLE `table name`;
The table will be deleted.
Queries:
Query is the most important part of sql. Till now we learnt how to create database, table using CREATE
DATABASE and CREATE TABLE statement, learnt how to insert data into table using INSERT INTO
statement and how to delete database and table using DROP DATABASE and DROP TABLE statement.
Now we will learn the use of SELECT statement.
Lets go back to our student table. Go to the SQL page. Lets say you want see all the data from your
student table. Write the query below:
SELECT*FROM `student`;
15 | P a g e
In short, SELECT* means all. So, here in this query we wanted to see all from the table student.
Now if you want to see particular column/columns from the database then all you have to do is to write
the column name/names after SELECT. In case of multiple columns you have to put commas to separate
them. Lets see an example,
See all first names from the table. Go to SQL and type:
SELECT `first_name` FROM `student`;
Image above shows how the result is going to look. Lets see multiple columns. Go to SQL and write the
query below:
SELECT `first_name`, `last_name`, `dept` FROM `student`;
16 | P a g e
SQL Operators
In the last query we used an operator (=) to find our required value. There is also some other operators
avileable for constracting a query. A list of operators are given below.
Operators
Definition
!=
<>
17 | P a g e
Greater than.
<
Less than.
>=
<=
LOGICAL OPERATORS
There are three Logical Operators AND, OR, and NOT. These operators compare two conditions at a time
to determine whether a row can be selected for the output. It allows you to combine more than one
condition.
AND:
OR:
NOT:
18 | P a g e
We can see the result from Figure 31. We got the result because both of our conditions were satisfied.
OR
Again find details of student/students whose last name is either Islam or student of BBA. Open SQL and
write,
SELECT * FROM `student` WHERE `last_name` = 'Islam'OR `dept` = 'BBA';
Or operator returns rows if any of the conditions is true. But if both the conditions are true, returns all
the rows that fulfills conditions.
NOT
Write the query in SQL page
19 | P a g e
As you can see, query returned all the rows without the row that contains Islam as lastname.
You can also construct complex queries combining multiple logical operators. Practice complex queries
yourself.
ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or descending
order. Mysql sorts query results in ascending order by default.
Write the query in your SQL page,
SELECT `first_name`, `last_name`, `dept` FROM `student` ORDER BY `first_name`;
Now, click GO button to execute the query.
20 | P a g e
Comparison Keyword
LIKE, BETWEEN.AND, IN, IS NULL are called comparison keywords in sql.These operators help to
enhance the search capabilities of sql.
LIKE
The LIKE operator is used to return all rows in a table whose column values match the specified pattern.
LIKE is very useful when do not know the entire value. We have to use wildcard character '%' to get our
desired result.
Now, Go to SQL page and write,
SELECT * FROM `student` where `first_name` LIKE Sh%;
Click GO button.
This query will show all the rows whose first name starts with Sh and % means rest of the letters in
the name can be any character. As you can see we have only one first name that starts with Sh, thats
why the query returned the row of Shajib.
There is another wildcard character which also used with LIKE. _ Underscore is used to signify a single
character with LIKE.
BETWEENAND
BETWEENAND operator is used to compare data for a specified range of values.
For example, we want to see student details whose ids are between 2 to 4. Lets write the query in SQL
page
SELECT * FROM `student` WHERE `id` BETWEEN 2 AND 4;
Click GO to see the result.
21 | P a g e
IN
The IN operator is used when we want to compare a column with more than one value. It is similar to
OR condition.
Lets say we want to see the students who is studying eitherBBA or CSE. Write the query in SQL,
SELECT `first_name`, `last_name`, `dept` FROM `student` WHERE `dept` IN(BBA, CSE);
Click GO button.
Result shows the list of students who studies BBA or CSE (Figure 47).
IS NULL
IS NULL shows the rows for columns that dont have any values. For example, if we want to find student
who dont have a last name then the query will be
SELECT `id`, `first_name`, `dept`FROM `student` WHERE `last_name` IS NULL;
Click GO button.
Our query returned an empty set, because we do not have any empty rows or columns. You can try this
query after inserting some new rows with empty values.
22 | P a g e
Lets say 3 of the students of student table gets a certain amount of financial aid from University each
month. Lets create a new table named aid to show the amounts. Write the query below to create
aid table
CREATE TABLE `aid` (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100),
`last_name` VARCHAR(100),
`amount` INT,
PRIMARY KEY(`id`)
);
Now, click GO to create new table.
As you can see our `aid` table is empty. So, first we have to inset data into it. Go to SQL page and write,
23 | P a g e
Figure 39 shows all the information of aid table we just inserted. But we were suppose to insert 3
students in our aid table, we mistakenly added Faisal in this list. So, we have to delete this entry.
DELETE
DELETE statement deletes specified information from a specified table. Lets delete Faisals information
from aid table. Go to SQL and write,
DELETE FROM `aid` WHERE `first_name` = `Faisal`;
Click GO button to delete the row.
We deleted the data we inserted mistakenly. Now if you go to aid table youll find only three names
listed.
ALTER TABLE
24 | P a g e
Here datatype means the type of data we are going to insert in this column (INT, VARCHAR etc.). If your
data type is VARCHAR then you have to set the character length inside brackets, which we have learnt
before.
Dropping column from table:ALTER TABLE
This statement above will drop the specified column from you specified table.
Changing Datatype of column:ALTER TABLE `table name` ALTER COLUMN `column name` DATATYPE;
This statement is used to change the datatype of a column.
Default:
In short, DEFAULT statement shows a default value if on data is inserted under a particular column. Lets go
back to our student table to get a better understanding about it. In student table our 4th column is dept,
which shows the departments of each student. Now, you want that dept column will show BBA for each
student if students department is not specified while inserting data into the dept column. DEFAULT can be
specified when you are creating a new table or you can use the ALTER TABLE statement to spacify the
default value for a column.
You could either write:
SQL JOINS
SQL Joins are used to relate information in different tables.A brief explanation of different types of joins
are given below:
JOIN: Return rows when there is at least one match in both tables
INNER JOIN:Returns rows when there is at least one match in both tables.
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
26 | P a g e
Figure 42: JOIN and INNER JOIN Query (Common field is not specified).
This time query returns 3 rows, which fulfills the condition (`student`.`first_name` = `aid`.`first_name`).
You must be wondering, what is this `student`.`first_name`? After ON we have to set the column
names from both the tables which contain identical values. But if the column names are same then
database shows error message. Thats why we have to put the table name before the column names
joined by a dot .. So, `student`.`first_name` means first_name is a column of student table. But you
can write only the column names if your column names are not same.
LEFT JOIN
Left join returnsall the values from the left table, even if there is no match found in the right table.
Write the query below:
SELECT * FROM `student` LEFT JOIN `aid` ON ( `student`.`first_name` = `aid`.`first_name` );
Click GO to see the result.
RIGHT JOIN
Right join returnsall the values from the right table, again, even if there is no match found in the left
table.
Write down the query:
SELECT * FROM `student` RIGHT JOIN `aid` ON ( `student`.`first_name` = `aid`.`first_name` );
Click GO.
28 | P a g e
We got this result because all the data of right table is available in the left table too.
SQL FUNCTIONS
SQL functions are built-in SQL functions that work on groups of rows and return one value for the entire
group. These functions are: COUNT, MAX, MIN, AVG, SUM, DISTINCT
COUNT()
COUNT()function returns the number of rows in the table that satisfies the condition specified in the
WHERE condition. If the WHERE condition is not specified, then the query returns the total number of
rows in the table.
Write the query below in you sql page:
SELECT COUNT(`last_name`) FROM `student`;
Click GO for see result.
MAX()
This function returns the maximum value from specified column. Write the query below.
SELECT MAX(`amount`) FROM `aid`;
Click GO.
29 | P a g e
MIN()
MIN() function returns the minimum value from specified column. Write this query
SELECT MIN(`amount`) FROM `aid`;
Click GO.
AVG()
This function calculates the average value from specified column. Type this query in sql,
SELECT AVG(`amount`) FROM `aid`;
Click GO to run the query.
SUM()
Im sure you already understood what this function does. It returns the sum of a numeric column. Type
the query below,
SELECT SUM(`amount`) FROM `aid`;
Cilck GO
30 | P a g e
31 | P a g e