0% found this document useful (0 votes)
11 views4 pages

SQL Commands for School Database Management

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

SQL Commands for School Database Management

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CREATE DATABASE

Command:

CREATE DATABASE SchoolDB;

Output:

Query OK, 1 row affected

USE DATABASE

Command:

USE SchoolDB;

Output:

Database changed

TO CREATE TABLE

Command:

CREATE TABLE Students (RollNo INT PRIMARY KEY, Name VARCHAR(50), Grade CHAR(1),

Marks INT);

Output:

Query OK, 0 rows affected

TO INSERT DATA

Command:

INSERT INTO Students VALUES (1, 'Aarav', 'A', 89), (2, 'Diya', 'B', 76), (3, 'Kunal', 'A', 91);

Output:

Query OK, 3 rows affected

TO VIEW TABLE

Command:

SELECT * FROM Students;

Output:

+--------+-------+-------+-------+
| RollNo | Name | Grade | Marks |

+--------+-------+-------+-------+

|1 | Aarav | A | 89 |

|2 | Diya | B | 76 |

|3 | Kunal | A | 91 |

+--------+-------+-------+-------+

SELECT COMMAND

Command:

SELECT Name, Marks FROM Students;

Output:

+-------+-------+

| Name | Marks |

+-------+-------+

| Aarav | 89 |

| Diya | 76 |

| Kunal | 91 |

+-------+-------+

USE DISTINCT CLAUSE

Command:

SELECT DISTINCT Grade FROM Students;

Output:

+-------+

| Grade |

+-------+

|A |

|B |
+-------+

CONDITIONAL SELECT / USING WHERE CLAUSE

Command:

SELECT * FROM Students WHERE Marks > 80;

Output:

+--------+--------+-------+-------+

| RollNo | Name | Grade | Marks |

+--------+--------+-------+-------+

|1 | Aarav | A | 89 |

|3 | Kunal | A | 91 |

+--------+--------+-------+-------+

ODER CLAUSE

Command:

SELECT * FROM Students ORDER BY Name ASC;

Output:

+--------+--------+-------+-------+

| RollNo | Name | Grade | Marks |

+--------+--------+-------+-------+

|1 | Aarav | A | 89 |

|2 | Diya | B | 76 |

|3 | Kunal | A | 91 |

+--------+--------+-------+-------+

USING BETWEEN OPERATOR

Command:

SELECT * FROM Students WHERE Marks BETWEEN 75 AND 90;

Output:
+--------+--------+-------+-------+

| RollNo | Name | Grade | Marks |

+--------+--------+-------+-------+

|1 | Aarav | A | 89 |

|2 | Diya | B | 76 |

+--------+--------+-------+-------+

Common questions

Powered by AI

To retrieve specific columns from a table, use the `SELECT` command followed by the desired column names. For instance, `SELECT Name, Marks FROM Students;` fetches only the 'Name' and 'Marks' columns, providing a view of each student's name and their corresponding marks .

The `BETWEEN` operator is used in a SQL query to select values within a given range. For example, to find students with Marks between 75 and 90, the query is `SELECT * FROM Students WHERE Marks BETWEEN 75 AND 90;`, which retrieves the rows for Aarav and Diya with Marks 89 and 76, respectively .

Primary keys ensure data integrity in SQL tables by enforcing the uniqueness of each record. In the 'Students' table, the 'RollNo' column is designated as the primary key, which means each student must have a unique RollNo. This prevents duplicate entries and maintains data consistency across the database .

To view all columns of a table, use the `SELECT *` command followed by the table name. For the 'Students' table, the command `SELECT * FROM Students;` will output all columns: RollNo, Name, Grade, and Marks, showing data for all students: Aarav, Diya, and Kunal .

To insert multiple records into a SQL table at once, use the `INSERT INTO` command followed by the table name and the `VALUES` keyword with multiple sets of parentheses, each containing a row of values. For instance, `INSERT INTO Students VALUES (1, 'Aarav', 'A', 89), (2, 'Diya', 'B', 76), (3, 'Kunal', 'A', 91);` inserts three records into the 'Students' table in a single statement .

To retrieve a list of students with marks above 80, you use the `SELECT` command with a `WHERE` clause to specify the condition. The SQL query `SELECT * FROM Students WHERE Marks > 80;` will return all rows where the 'Marks' column has a value greater than 80, showing students Aarav and Kunal in this case .

You use the `SELECT DISTINCT` command to display unique values from a specific column. The command retrieves unique entries by filtering out duplicate values. For instance, `SELECT DISTINCT Grade FROM Students;` returns only unique grades from the 'Grade' column, which in this case are 'A' and 'B' .

Conditional filtering in SQL is implemented using the `WHERE` clause to specify criteria for data selection. To select students by specific grades, for example, 'A' grades, use `SELECT * FROM Students WHERE Grade = 'A';`, retrieving only students Aarav and Kunal. This filters the table based on the desired condition, allowing for refined data queries .

Results in a SQL table are sorted using the `ORDER BY` clause, which arranges the retrieved data in ascending or descending order based on one or more columns. The default order is ascending (ASC). For example, `SELECT * FROM Students ORDER BY Name ASC;` will sort the data on the 'Name' column in alphabetical order .

To create a new database, you use the SQL command `CREATE DATABASE`, followed by the database name. For example, `CREATE DATABASE SchoolDB;` creates a new database named 'SchoolDB'. After creating the database, you can switch to it using the `USE` command followed by the database name, as in `USE SchoolDB;` .

You might also like