We were asked to create a SQL statement to show the count of customers whose country is USA. The column name of the result set should then be renamed into numcustomers
.
The database is in here: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all.
I already found the count(*) of customers in the USA:
SELECT *
FROM Customers
WHERE Country = 'Germany'
But I can't figure out how to rename COUNT(*)
into numcustomers
. Any help?
If we want to change the 'city' column with the new name 'city_name' of this table, we can use the above-specified SQL Server syntax or stored procedure as follows: EXEC SP_RENAME 'Student. city', 'city_name', 'COLUMN'
1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.
COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.
*Syntax may vary in different databases. Syntax(Oracle,MySQL,MariaDB): ALTER TABLE table_name RENAME TO new_table_name; Columns can be also be given new name with the use of ALTER TABLE.
SELECT count(*) AS numcustomers FROM Customers
WHERE Country = 'USA'
Use AS
keyword to set column alias.
Akshey Bhat is right
SELECT
count(*) AS numcustomers
FROM Customers
WHERE Country = 'USA'
This query to get a number of usa customer and this index name is numcustomers or column name is numcustomers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With