You are currently viewing SQL Introduction with Examples

SQL Introduction with Examples

Share this to everyone:

SQL, or Structured Query Language, is a language used to communicate with and manage databases. It’s like a special language that helps us ask questions and give instructions to databases in an organized way. With SQL, we can create, modify, and retrieve information from databases.

Imagine a database as a big table with rows and columns. SQL allows us to ask questions like “Show me all the information in the ‘Customers’ column” or “Add a new row to the ‘Orders’ column.” It helps us store, update, and retrieve data efficiently.

In simpler terms, SQL is a way to talk to databases and get the information we need from them, like asking questions and giving instructions in a language that both humans and databases can understand.

Examples of SQL commands:
1.Create a database named student
Create Database Student;
2.Create a table student(Roll,Name, Address,Age)
Create table student(Roll int,Name varchar(200),
Address varchar(200), Age int);
3.Insert details of any 3 students.
Insert into student values(1,”Ram”,”Damak”,35);
Insert into student values(2,”Raju”,”Urlabari”,30);
Insert into student values(3,”Sita”,”Damak”,20);

  1. Select all details of all students.
    Select * from student;
  2. Select Name of all students.
    Select Name from student;
  3. Select Name of the student whose roll is 2.
    Select Name from student where Roll=2;
  4. Select Name and Address of those students who lives in “Damak”.
    select Name,Address from student where Address=”Damak”;
  5. Update Name of student whose Roll is 3 & Delete records of Raju.
    Update student set Name=”Suresh” where Roll=3;
    Delete from student where Name=”Raju”;
  6. Add new column Gender.
    Alter table student add column Gender varchar(200);
  7. Update the gender of students & Select name of those students whose name end at a.
    Update student set Gender=”Male” where Roll=1;
    Update student set Gender=”Male” where Roll=3;
    Select Name from student where Name like ‘%a’;
  8. Select name of all students in descending order.
    Select Name from student order by name desc;
  9. Select age of all students in ascending order according to roll & Count the number of students.
    Select age from student order by roll asc;
    select count(roll) from student;
  10. count the number of student who live in Damak.
    select count(Address) from student where Address=”Damak”;
  11. Select total age.
    select sum(age) from student;
  12. Select average age.
    select avg(age) from student;
  13. Select minimum age.
    select min(age) from student;
  14. Select maximum age.
    select max(age) from student;
  15. Select all details of the student whose age is minimum.
    select * from student where age=(select min(age) from student );
  16. Select Name and address of the student whose age is maximum.
    select Name,Address from student where Roll=(select max(Roll) from student);
  17. Delete the table:
    Drop table student;
  18. Delete the database;
    Drop database student;

OLD QUESTION 2079:

  1. Write SQL DDL commands to execute the following task with reference to the schema
    given below:
    student_info(regno as integer,name as character(25),class integer,
    gender character(1), address character(25).
    Answer:

    To execute the given task based on the provided schema, you can use SQL DDL (Data Definition Language) commands to create the table “student_info” with the specified columns. Here’s the SQL code:
    CREATE TABLE student_info (
    regno INT,
    name CHAR(25),
    class INT,
    gender CHAR(1),
    address CHAR(25)
    );

    In this code, we use the CREATE TABLE statement to define the table “student_info” with the required columns. The columns are defined with their respective names and data types: “regno” as an integer (INT), “name” as a character with a maximum length of 25 (CHAR(25)), “class” as an integer (INT), “gender” as a single character (CHAR(1)), and “address” as a character with a maximum length of 25 (CHAR(25)).

Practice yourself:

Consider the same table given above and write SQL for the followings:
1)Insert any 5 records.
2) Display the record according to name.
3) Count the number of male students.
4) Display the record of those students who live in Damak.
5) Set the class of all students to 11.
6) Update the registration number of any one student.
7) Delete the record of “Kumar”.

[Comment your answer in the comment section]