Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
4
votes1
answer514
viewsSelect data from two tables to display in one column?
Consider the tables for customer registration : Table Person |ID|NOME|TIPO|EMAIL| Table Person Physical |ID|CPF| Person Juridical Table |ID| CNPJ| inscricao_municipal| inscricao_estadual| My goal is…
-
4
votes1
answer55
viewsHow do I change everything to a single php value
My question is this:: In a table, I have a column designated nomecliente. In this table I have several records, however the column nomecliente of the records are different from each other. How could…
-
4
votes2
answers40
viewsHow to change the value of fields in a column?
I am with a simple doubt but I could not find a solution. The same way I can when I use select bring an empty column using select a.x,NULL as y,a.z from DADOS a, how can I make a column instead of…
-
4
votes3
answers670
viewsHow to sort sql queries in the RUBY on Rails model?
I have the following method an ROR application. query_student = "SELECT name, registration, room FROM students WHERE registration = "+ params[:registration]+" AND password = "+params[:password]…
-
4
votes2
answers272
viewsQuery in two tables at the same time
I have for example the following scenario: Table Coluna01 Coluna02 Coluna03 book id name gender captl id name content cplivro id idcap idlivro When I have to associate a book with a chapter I use…
-
4
votes2
answers8268
viewsSelect with the day of the week in Portuguese
Good afternoon Guys, I need to make a select that translates the day of the week into Portuguese, it is already working, but in English. How can I change. Select *,id, data,…
-
4
votes1
answer308
viewsHow to cast exception in SQL and treat in C#
I’m creating procedures in an SQL Server database. To clear a brand, I first check if the brand is already tied to a product before deleting. CREATE PROCEDURE SP_Delete_MarcasProdutos @Id int AS…
-
4
votes1
answer139
viewsRecover last row inserted in table with non-sequential primary key - Mysql
It is possible to know which was the last row inserted in a Mysql table where the primary key is not sequential? I have a table where the primary key consists of two columns that are FK, so they…
-
4
votes1
answer373
viewsHow to check if there is scheduling in a time range?
I need to create an agenda where I allocate a service provider to work for a client. For example: Prestadordeservicos João will provide services to the customer condominium valley of the sun on the…
-
4
votes1
answer2544
viewsSelect last JOIN record
I have a table tb_processos and another tb_detalhes (saves details of a particular process), would like to take all the data of the process and only the last detail of the process. The way I’m doing…
-
4
votes1
answer362
viewsOrder of execution of Migrations
Hello! In my first job, I worked using code-first with Entity Framework 4. At this time, I noticed a limitation in it: the generated Migrations only ran in order. For example, if in my branch two…
-
4
votes3
answers372
viewsList dates calculated per month based on parameters
Based on two dates received per parameter, I am trying to list all dates per month (one date per month) based on the start date and up to the end date. These dates returned have to check the day of…
-
4
votes1
answer4360
viewsHow to remove the UNIQUE KEY attribute from a column in Mysql?
I created a table in Mysql that uses a column called loja_num with the attribute key dish UNIQUE KEY loja_num (loja_num, But now, I would like to remove this attribute from this column. I have…
-
4
votes2
answers833
viewsVarchar or Datetime?
I always save my bank dates using the type datetime. When displaying these dates in an application, I do something like: select Convert(Varchar, GETDATE(),103) Data Upshot: 04/10/2016 I’d like to…
-
4
votes1
answer3172
viewsHow to concatenate data into sql server
I have a field where the date and time is recorded: '2013-06-13 00:00:01' and '2013-06-13 11:59:59' I want to make an appointment by passing the date and a fixed time: CONVERT(VARCHAR(10),…
-
4
votes1
answer132
viewsHow to create foreach (PHP/SQL)
I’m trying to send from a form html, a table, with two fields with Array[]. These two fields are Service and Value, I want to add more than one product on form, and he insert into different records…
-
4
votes1
answer274
viewsSQL return in different columns, not rows
I am testing a query, where I would need the returns to come in separate columns, but the way below it returns in rows. I tried to use LEFT JOIN but the syntax didn’t work. select u.nome_completo AS…
-
4
votes1
answer857
viewsVarchar or int for "type" fields
I have a column guy with 6 options to save in the bank, it is better to save the string as varchar or as int and treat it in front? For example, when it is 1 in the bank, display certain string on…
-
4
votes1
answer2735
viewsCalculate with select output
How can I make a calculation in select below? I made the sums of quantities and values, now I need to take these sums and make a balance. Would that be: qtd_type_0 - qtd_type_1 - qtd_type_2 and…
-
4
votes3
answers75
viewsHow can I exclude the penultimate number from a period?
I’m trying to leave the period from 201601 to 20161 in SQL, but I can not at all! I wonder if someone could help me? SELECT CONVERT(NUMERIC(5), CONVERT(VARCHAR(4), YEAR(Periodo_Mensal))+…
-
4
votes1
answer145
viewsGenerate XML content HASH in SQL
Is there a way to generate the hash of a XML beyond the function master.sys.fn_repl_hash_binary? Currently this is my implementation, however, I do not know if it is the only way to do. --Preparação…
-
4
votes1
answer368
viewsHow to remove duplicated rows by holding other columns
In the example below, I am interested in removing duplicates of rows in columns title and time no matter how many columns. In the example below I am interested that the query returns rows 32, 34 and…
-
4
votes1
answer89
viewsHow can I get the BD date already formatted using the Max and Date_format functions together
I’m trying to get the biggest date registered in my BD, I am trying to use the functions MAX and DATE_FORMAT together and the search result is not what expected, I have these dates registered:…
-
4
votes3
answers820
views -
4
votes2
answers2957
viewsI need a new Random number for each line in an sql(server) query
I use the sql server. I need to use a Random number in several different columns (the same Random number) but I want a new Random number for each row... for example, let’s assume that TABELAX has…
-
4
votes5
answers29127
viewsuse if in sql server
I have 3 tables in my database, CUSTOMERS, DISTRIBUTORS and EXPORT and I have a production order table where I take the customer type (Distributor, customer or export) and the ID of each of them.…
-
4
votes1
answer382
viewsSize occupied on disk by a table in Sqlite
In Oracle, when we want to know how much a table occupies disk we can make a query to the dictionary dba_segments. SELECT segment_name, segment_type, bytes/1024/1024 MB FROM dba_segments WHERE…
-
4
votes2
answers1104
viewsOracle SQL result ordering
I am done the query below, which was ordered by the date of creation of the product batch. With the migration of version 11 to R12 of Oracle, the batch creation field became the migration date, that…
-
4
votes1
answer249
views -
4
votes1
answer3517
viewsDefault error for datetime field in Mysql
I had a problem trying to run my database script on the server. The error already gives in the first table that the script generates: CREATE TABLE IF NOT EXISTS `categoria` ( `cd_categoria` int(255)…
-
4
votes1
answer2366
viewsSQL Count and sum
I have 6 SQL codes and all search in the same table, it looks like this: +-----+-----+-----+ | A | B | C | +-----+-----+-----+ | 1 | 1 | 1 | +-----+-----+-----+ | 2 | 1 | 5 | +-----+-----+-----+ | 3…
-
4
votes2
answers2287
viewsRename table in Firebird
How to rename table in firebird 2.5? I’m using the query: ALTER TABLE cliente RENAME TO clientes;, but error appears: invalid Toke 'Rename'.…
-
4
votes2
answers125
viewsHow to remove rows from a table A that has no relationship to table B?
I have 2 tables and want to remove the rows of table A that has no relationship with table B? For example: In table B I have a field FK_ID and I want to remove from table A all rows that have no…
-
4
votes1
answer5678
viewsReturn higher value of a specific SQL record
I need my SQL to return the longest date, but not the longest date among all records, and yes, the longest date of a specific id. I’ve tried using the MAX(), but it returns the highest value of all…
-
4
votes2
answers12845
viewsHow to get the names of all Postgresql database tables?
How to get the names of all Postgresql database tables? How to get the attributes(code, name, etc...) of a given table in the Postgresql database?
-
4
votes3
answers162
viewsProblems with SELECT with 2 Wheres
I’m having a bit of an amateur problem here. I’m not getting this SELECT from 2 CONDITIONS, and I can’t find the ERROR! Someone please give me a light there! Follows the Code: OleDbConnection Con =…
-
4
votes2
answers310
viewsWhich object performs best? Sqldatasource or Datatable?
In terms of performance, speed, or even safety, what is the best object to be used as a DataSource? Example, I’m creating a DataSource, to popular a GridView: For SqlDataSource:…
-
4
votes1
answer1453
viewsDoubt in the method of using Distinct and Count together!
I have a requested table as the example below: Id | Cliente | Status 1 | XPTO | Proposta 2 | ABCD | Proposta 3 | XPTO | Venceu 4 | XPTO | Perdeu And I want to present a result like this: Cliente |…
-
4
votes4
answers6919
viewsSELECT of month/year in DATES
I need select to return all the rows that were inserted in July 2010, as shown in the example, regardless of the day. How to return this query? SELECT * FROM FRCAST WHERE DATA_ID =…
-
4
votes1
answer164
viewsRelationship (Database) (Sql)
kindly: I can’t understand the following case: My problem would be the syntax used in part (1,1) -- (1,N) In case it would be, 1 Department allots N employees and 1 Employee can be in only 1…
-
4
votes2
answers1297
viewsRetrieve data from another mysql table and quantity
I’m having a lot of doubts about Mysql when I have to use INNER JOIN to retrieve data from other tables. I have 3 tables: MEDIA id, user_id, title, Description COMMENTS id, user_id, media_id,…
-
4
votes2
answers184
viewsHow to use the IN clause in Lambda?
A doubt arose that I thought would be easy, but the content I found could not understand. Well, here’s the thing: I have the tables "Client" and "Errosproducao_client" where they relate form 1 to N,…
-
4
votes2
answers907
viewsHow to work with lock in SQL records?
I’m studying best practice to work with lock of records. What I mean is, I have several tables that can be accessed by several users simultaneously, but if any attempt is made to edit the record,…
-
4
votes2
answers657
viewsHow to list NULL on a LEFT JOIN even if it returns joins?
The situation found was that: I am making a junction between representatives and sellers, and in another situation this junction is made with another sales. There are sales without sellers but are…
-
4
votes1
answer808
viewsSelect to compare value sets from columns of two tables
Sirs, First, I have 2 tables Table 1: ID L1 L2 L3 L4 L5 ------------------------------------ 1 a c e g i 2 a c d g i 3 l n p r t Table 2: ID L1 L2 L3 L4 L5 L6…
-
4
votes1
answer11299
viewsCluster and Sum in Oracle
I have a customer record ID NOME GRUPO 2033 JOAO FORNECEDORES 2044 MARIA MANUTENCAO 2055 JOSE FORNECEDORES And I have a record of purchases made by each customer ID_CLIENTE VALOR_COMPRA 2033 4.000…
-
4
votes1
answer75
viewsSql group records
I have the following appointment: select T1.dt_producao, T1.cod_turno, T1.cod_periodo, CASE WHEN T2.TP_PALLET = 'DZ' THEN sum(T1.QT_PROD* T2.QT_EMBALAGEM*12) ELSE sum(T1.QT_PROD* T2.QT_EMBALAGEM)…
-
4
votes1
answer539
viewsWhat’s the difference when you use Inary in the Where clause?
What is Binary and what he does? What is the difference generated when using the Binary in a query in the Mysql and when it does not use the Binary? SELECT email, senha from login WHERE usuario = ?…
-
4
votes1
answer116
viewsHow to pass an array in a WHERE condition
$this->db->set('al_reg', $matricula); $this->db->set('dataagendamento', $dataagendamento); $this->db->where('cod_lab', $laboratorio); $this->db->where('cod_horario',…
-
4
votes1
answer3970
viewsHow to calculate a person’s age in Oracle SQL?
I need a sql command that returns the age of the person. I have the birth date of the person in the database, I need to calculate his age by SQL.